Cod sursa(job #3370998)

Utilizator matei__bBenchea Matei matei__b Data 28 octombrie 2026 11:16:46
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
// https://www.infoarena.ro/problema/sate

#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=3e4+8;

int n,m,src,dest;
vector<pair<int,int> > g[NMAX];

int dist[NMAX];

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n >> m >> src >> dest;
    for(int i=1; i<=m; i++) {
        int x,y,w;
        fin >> x >> y >> w; 

        g[x].push_back({y,w});
        g[y].push_back({x,-w});
    }

    queue<int> q;

    q.push(src);
    while(!q.empty()) {
        int nod=q.front();
        q.pop();

        for(const auto &[vec,w]:g[nod]) {
            if(dist[vec]!=0) {
                continue;
            }

            dist[vec]=dist[nod]+w;
            q.push(vec);
        }
    }

    fout << dist[dest] << "\n";

    return 0;
}