Cod sursa(job #2840796)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 28 ianuarie 2022 19:08:46
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

struct edge {
    int nod, dist;
};

int dist[30030];

int main ()
{
    int n, m, x, y;
    int a, b, c;
    in >> n >> m >> x >> y;
    vector<vector<edge>> adj;
    adj.resize(n + 1);
    for (int i = 1; i <= m; i++){
        in >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, -c});
    }
    queue<edge> q;
    q.push({x, 0});
    while (!q.empty())
    {
        edge crt = q.front();
        q.pop();
        for (auto edge : adj[crt.nod])
        {
            if (!dist[edge.nod])
            {
                dist[edge.nod] = crt.dist + edge.dist;
                q.push({edge.nod, dist[edge.nod]});
                if (edge.nod == y) break;
            }
        }
    }
    out << dist[y];
}