Cod sursa(job #700818)

Utilizator avram_florinavram florin constantin avram_florin Data 1 martie 2012 12:06:30
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include<fstream>
#include<cstdio>
#include<vector>
#include<queue>

using namespace std;

const int MaxN = 30001;

const char InFile[] = "sate.in";
const char OutFile[] = "sate.out";

int N,M,X,Y,d[MaxN];
bool viz[MaxN];
vector< pair<int,int> > G[MaxN];
queue<int> Q;

void BFS()
{
	int nod;
	vector< pair<int,int> >::iterator it,iend;
	viz[X] = true;
	Q.push(X);
	while( !Q.empty() )
		{
			nod = Q.front();
			Q.pop();
			it = G[nod].begin();
			iend = G[nod].end();
			for( ; it != iend ; ++it )
				if( !viz[it->first] )
					{
						viz[it->first] = true;
						d[it->first] = d[nod] + it->second;
						Q.push(it->first);
						if( it-> first == Y )
							return;
					}
		}
}

int main()
{
	ifstream fin( InFile );
	ofstream fout( OutFile );
	fin >> N >> M >> X >> Y;
	int i,x,y,cost;
	for( i = 0 ; i < M ; ++i )
		{
			fin >> x >> y >> cost;
			G[x].push_back(make_pair(y,cost));
			G[y].push_back(make_pair(x,-cost));
		}
	BFS();
	fout << d[Y] << '\n';
	fin.close();
	fout.close();
	return 0;
}