Cod sursa(job #483298)

Utilizator petroMilut Petronela petro Data 7 septembrie 2010 21:57:22
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<fstream>
#include<vector>
#include<queue>
#define M 30005
#define pb push_back
#define mp make_pair
using namespace std; 

ifstream f("sate.in");
ofstream g("sate.out");

typedef vector< pair<long, long> > VI;
VI a[M];
VI::iterator it;
queue<long> q;
long n,m,X,Y,d[M];

void cit()
{
	long i,x,y,z;
	f>>n>>m>>X>>Y;
	
	for(i=1;i<=m;++i)
	{
		f>>x>>y>>z;
		a[x].pb(mp(y,z));
		a[y].pb(mp(x,z));
	}
	f.close();
}

void drum()
{
	long x,y,z,i;
	
	for(x=1;x<=n;++x)
		d[x]=-1;
	
	d[X]=0;
	q.push(X);
	
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		if(x==Y) break;
		for(it=a[x].begin(); it!=a[x].end(); ++it)
		{
			y=(*it).first;
			z=(*it).second;
			
			if(d[y]==-1) {if(y>x) d[y]=d[x]+z;
						  else d[y]=d[x]-z;
						  q.push(y);}
		}
	}
	
}

int main()
{
	cit();
	drum();
	g<<d[Y]<<"\n";
	g.close();
	return 0;
}