Cod sursa(job #2488918)

Utilizator hoprixVlad Opris hoprix Data 7 noiembrie 2019 19:50:58
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 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];
bool 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();
        pair <int, int> i;
        for(unsigned int j = 0; j < A[sat].size(); ++j) {
            i = A[sat][j];
            if(!viz[i.first]) {
               viz[i.first] = 1;
               d[i.first] = d[sat] + i.second;
               if(i.first == Y) {
                   fout << d[Y];
                   return;
               }
               q.push(i.first);
            }
        }
    }
}

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