Cod sursa(job #2489819)

Utilizator hoprixVlad Opris hoprix Data 9 noiembrie 2019 12:05:58
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAX = 30005;
int N, M, X, Y, d[MAX], viz[MAX];
vector < pair <int, int> > A[MAX];
queue <int> q;

void solve() {
    q.push(X);
    viz[X] = 1;
    while(!q.empty()) {
        int sat = q.front();
        q.pop();
        for(auto i : A[sat])
            if(!viz[i.first]) {
                viz[i.first] = 1;
                q.push(i.first);
                d[i.first] = d[sat] + i.second;
                if(i.first == Y) return;
            }
    }
}


int main() {
    fin >> N >> M >> X >> Y;
    int a, b, cost;
    while(M--) {
        fin >> a >> b >> cost;
        A[a].push_back({b, cost});
        A[b].push_back({a, -cost});
    }
    solve();
    fout << d[Y];
}