Cod sursa(job #2489238)

Utilizator hoprixVlad Opris hoprix Data 8 noiembrie 2019 09:38:17
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MAX = 30005;
const int INF = 2e7+1;
int N, M, X, Y, d[MAX], sat, i;
vector < pair <int, int> > A[MAX];
queue <int> q;

void solve() {
    for(int k = 1; k <= N; ++k)
        d[k] = INF;
    d[X] = 0;
    q.push(X);
    while(!q.empty()) {
        sat = q.front();
        q.pop();
        for(unsigned int j = 0; j < A[sat].size(); ++j) {
            i = A[sat][j].first;
            if(d[sat] + A[sat][j].second < d[i]) {
               d[i] = d[sat] + A[sat][j].second;
               q.push(i);
            }
        }
    }
}


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];
}