Cod sursa(job #1053464)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 12 decembrie 2013 19:22:52
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.99 kb
#include<cstdio>
#include<vector>
#include<queue>

using namespace std;

#define Nmax 100025

int cost[Nmax], N, M, X, Y;
vector<pair<int, int> > G[Nmax];

void BFS(int start) {
    queue<int> Q;
    int dist, nod, x;

    Q.push(start);
    while(!Q.empty()) {
        x=Q.front();
        for(vector<pair<int, int> >:: iterator it=G[x].begin(), it2=G[x].end(); it!=it2; ++it) {
            nod=it->first; dist=it->second;
            if(!cost[nod]) {
                cost[nod]=cost[x]+dist;
                if(nod==Y)
                    return;
                Q.push(nod);
            }
        }
        Q.pop();
    }
}

int main() {
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);

    int x, y, c;

    scanf("%d %d %d %d",&N,&M,&X,&Y);
    while(M--) {
        scanf("%d %d %d",&x,&y,&c);
        G[x].push_back(make_pair(y,c));
        G[y].push_back(make_pair(x,-c));
    }
    BFS(X);
    printf("%d\n",cost[Y]);

    return 0;
}