Cod sursa(job #3335108)

Utilizator Stefanstef99Stefan Puica Stefanstef99 Data 21 ianuarie 2026 17:31:51
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, s, f;
vector < pair<int,int> > g[30005];
int viz[30005], dist[30005];
queue <int> q;

int main()
{
    int i, x, y, d;
    fin >> n >> m >> s >> f;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> d;
        g[x].push_back({y, d});
        g[y].push_back({x, -d});
    }
    q.push(s);
    viz[s] = 1; dist[s] = 0;
    while(!q.empty())
    {
        x = q.front();
        q.pop();
        for(auto e : g[x])
        {
            y = e.first;
            d = e.second;
            if(viz[y] == 0)
            {
                dist[y] = dist[x] + d;
                viz[y] = 1;
                q.push(y);
            }
        }
    }
    fout << dist[f] << '\n';
    return 0;
}