Cod sursa(job #2488890)

Utilizator hoprixVlad Opris hoprix Data 7 noiembrie 2019 19:32:57
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX = 30005;
int N, M, X, Y, dist;
bool viz[MAX];
vector < pair <int, int> > A[MAX];

int solve() {
    queue <int> q;
    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;
               dist += i.second;
               if(i.first == Y) return dist;
               q.push(i.first);
            }
        }
    }
}

int main() {
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    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});
    }
    fout << solve();
}