Cod sursa(job #3272291)

Utilizator mateistefan11matei stefan mateistefan11 Data 29 ianuarie 2025 08:23:46
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector< pair<int, int> > G[30005];
int n,m, x, y;
int d[100025], viz[100025];
void BFS(int x)
{
    int t;
    queue<int> q;
    q.push(x);
    d[x] = 0;
    viz[x] = 1;
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        for(auto e : G[t])
            if(!viz[e.first])
            {
              viz[e.first] = 1;
               if(e.first > t)
                    d[e.first] = d[t] + e.second;
               else
                    d[e.first] = d[t] - e.second;
                q.push(e.first);
            }
    }
}
int main()
{
    int i,j,c;
    fin >> n >> m >> x >> y;
    while(m--)
    {
        fin >> i >> j >> c;
        G[i].push_back({j, c});
        G[j].push_back({i, c});
    }
    BFS(x);
    fout << d[y];
    return 0;
}