Cod sursa(job #1163223)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 1 aprilie 2014 11:26:33
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<cstdio>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");

const int nmax = 30009;
vector <int> v[nmax], vcost[nmax];
int n, m, x, y; 
int vdist[nmax];
bool poz[nmax];

void bfs( int n ) {
    queue <int> q;
    poz[n] = 1;
    q.push( n );
 
    while ( !q.empty() ) {
        int k = q.front();
        q.pop();
        for ( int i = 0; i < (int)v[k].size(); ++ i ) {
            if ( poz[ v[k][i] ] == 0 ) {
                poz[ v[k][i] ] = 1;
                vdist[ v[k][i] ] = vdist[k] + vcost[k][i];
                q.push( v[k][i] );
            }
        }
    }
}

int main(){

	in>>n>>m>>x>>y;
	for(int i = 0; i<m; ++i)
	{
		int a, b, cost;
		in>>a>>b>>cost;

		v[a].push_back(b);
		v[b].push_back(a);

		vcost[a].push_back(cost);
		vcost[b].push_back(-cost);
	}

	bfs(x);

	out<<vdist[y];
	return 0;
}