Cod sursa(job #3272319)

Utilizator inacioataCioata Ana Irina inacioata Data 29 ianuarie 2025 09:22:44
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("n.in");
ofstream fout("n.out");
int n, m, x, y, d[100005], viz[100005];
vector<pair<int, int>> L[30005];

void BFS(int x)
{
    int k;
    queue<int> q;
    q.push(x);
    viz[x] = 1; d[x] = 0;
    while(!q.empty())
    {
        k = q.front();
        q.pop();
        for(auto i : L[k])
            if(viz[i.first] == 0)
            {
                viz[i.first] = 1;
                if(i.first > k) d[i.first] = d[k] + i.second;
                else d[i.first] = d[k] - i.second;
                q.push(i.first);
            }
    }
}
int main()
{
    int i,j,c;
    fin >> n >> m >> x >> y;
    while(fin >> i >> j >> c)
    {
        L[i].push_back({j, c});
        L[j].push_back({i, c});
    }
    BFS(x);
    fout << d[y];
    return 0;
}