Cod sursa(job #1395152)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 21 martie 2015 01:32:00
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define inf (1<<28)
#define node second
#define value first
#define nmax 30005
using namespace std;

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

vector <pair<int, int> > v[nmax];
queue <int> q;

int n, m, x, y;
int best[nmax];

void explore(int x){
    for(int i=1; i<=n; i++) best[i]= inf;
    best[x]= 0;
    q.push(x);
    while(!q.empty() || best[y] == inf){
        int cur= q.front();
        q.pop();
        for(int i=0; i<v[cur].size(); i++){
            if(best[v[cur][i].node] > best[cur]+v[cur][i].value){
                best[v[cur][i].node]= best[cur]+v[cur][i].value;
                q.push(v[cur][i].node);
            }
        }
    }
}

int main(){
    fin.sync_with_stdio(false);
    int a, b, c;
    fin >> n >> m >> x >> y;
    for(int i=1; i<=m; i++){
        fin >> a >> b >> c;
            v[a].push_back(make_pair(c, b));
            v[b].push_back(make_pair(-c, a));
    }
    explore(x);
    fout << best[y] << " ";
    return 0;
}