Cod sursa(job #1498252)

Utilizator DobosDobos Paul Dobos Data 8 octombrie 2015 10:55:42
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX = 30005;
vector < pair <int ,int > > G[MAX];
deque < int > lx;
int D[MAX];
inline int bfs(int f)
{
    int x;
    while(!lx.empty()){
        x = lx.front();
        lx.pop_front();
        for(int i = 0; i < G[x].size(); i ++){
            if(D[G[x][i].first] == 0){
                D[G[x][i].first] = D[x] + G[x][i].second;
                lx.push_back(G[x][i].first);
                if(G[x][i].first == f)
                    return 0;
            }
        }
    }
}
int main()
{
    int n,m,s,f,x,y,d;
    fin >> n >> m >> s >> f;
    for(int i = 0 ; i < m; i ++){
        fin >> x >> y >> d;
        if(x < y)
        G[x].push_back(make_pair(y,d));
        G[y].push_back(make_pair(x,-d));
    }
    lx.push_back(s);
    bfs(f);
    fout << D[f] ;

    return 0;
}