Cod sursa(job #957145)

Utilizator lucky1992Ion Ion lucky1992 Data 4 iunie 2013 16:20:28
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <list>
#include <vector>
#include <queue>

#define MAX 300002
#define fin "sate.in"
#define fout "sate.out"

using namespace std;


typedef pair<int,int> node;

int N,M,S,D;
list<node> edges[MAX];
vector<int> dist( MAX, 0 );
queue<int> q;
vector<bool> visited(MAX,0);

int main(){

	int x, y, cost;

	ifstream in(fin);
	ofstream out(fout);

	in >> N >> M >> S >> D;

	for( int i = 1; i <= M; i++ ){
	    in >> x >> y >> cost;
	    edges[x].push_back( node(y, cost));
            edges[y].push_back( node(x, -cost));
	}
	
	visited[S] = 1;
	q.push(S);
	while( !q.empty() && !visited[D] ){
	
	     int u = q.front();
	     q.pop();
	     
	     for( auto it = edges[u].begin(); it != edges[u].end(); it++ ){
		pair<int,int> nod = *it;	
		if( visited[ nod.first] == 0 ){
		  visited[nod.first] = 1;
		  dist[nod.first] = dist[u] + nod.second;
		  q.push(nod.first);
		}
	    }
	    
	     if( dist[D] != 0 )
		break;
	}
	
	out<< dist[D];
	in.close();
	out.close();

	return 0;
	}