Cod sursa(job #3370942)

Utilizator Ciurea_VladCiurea Eduard-Vlad Ciurea_Vlad Data 27 octombrie 2026 23:34:43
Problema Sate Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,x,y,a,b,d,distanta_fin;
vector<int>poz;
vector<vector<pair<int, int>>>graph;
void bfs(int node)
{
    queue<int>q;
    q.push(node);
    while(!q.empty())
    {
        node=q.front();
        q.pop();
        for(auto it : graph[node]) {
            int next=it.first;
            int d=it.second;
            if(poz[next] == -1)
            {
                if (next>node)
                    poz[next]=poz[node]+d;
                if (next<node)
                    poz[next]=poz[node]-d;
                q.push(next);
            }
        }
        if (node == b)
            break;
    }
}
int main()
{
    f>>n>>m>>a>>b;
    poz.assign(n+1,-1);
    graph.assign(n+1,vector<pair<int,int>>(m+1,{0,0}));
    for(int i=1; i<=m; i++)
    {
        f>>x>>y>>d;
        graph[x].push_back({y,d});
        graph[y].push_back({x,d});
    }
    poz[a] = 0;
    bfs(a);
    g<<poz[b];

    return 0;
}