Cod sursa(job #2722073)

Utilizator codrut86Coculescu Ioan-Codrut codrut86 Data 12 martie 2021 16:16:12
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream in("sate.in");
ofstream out("sate.out");

const int N = 3e4 + 1;
int n, m, x, y, d[N];
vector <pair<int, int>> G[N];
queue <int> q;

void bfs(int n)
{
    q.push(n);

    while(!q.empty())
    {
        int x = q.front();
        q.pop();

        for(auto y : G[x])
        {
            if(d[y.first] == 0)
            {
                d[y.first] = d[x] + y.second;
                q.push(y.first);
            }
        }
    }
}

int main()
{
    in >> n >> m >> x >> y;

    for(int i=1; i<=m; i++)
    {
        int a, b, dist;
        in >> a >> b >> dist;
        G[a].push_back({b,dist});
        G[b].push_back({a,-dist});
    }

    bfs(x);
    out << d[y] << " ";
    return 0;
}