Cod sursa(job #456370)

Utilizator bog29Antohi Bogdan bog29 Data 15 mai 2010 13:44:53
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include<fstream>
#include<vector>
#include<queue>
#define dmax 28005
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");

int n,m,s,f,ok;
bool t[dmax];
long long dist[dmax];

struct drum
{	short int v;
	int c;
};	

vector<struct drum>g[dmax];
vector<struct drum>::iterator it;
queue<int>q;

void bfs()
{	int i,crt;
	q.push(s);
	t[s]=1;
	ok=1;
	while(!q.empty() && ok)
	{	crt=q.front();
		q.pop();
		if(crt==f)
		{	out<<dist[crt];
			ok=0;
		}
		for(it=g[crt].begin();it<g[crt].end();it++)
			if(!t[it->v])
			{	if(it->v > crt)
					dist[it->v]=dist[crt]+it->c;
				else dist[it->v]=dist[crt]-it->c;
				q.push(it->v);
			}	
	}
}

int main()
{	long long i,a,b,ct;
	drum z;
	in>>n>>m>>s>>f;
	if(s>f)
	{	i=f;
		f=s;
		s=i;
	}	
	for(i=1;i<=m;i++)
	{	in>>a>>b>>ct;
		z.v=b;
		z.c=ct;	
		g[a].push_back(z);
		z.v=a;
		g[b].push_back(z);
	}
	in.close();
	bfs();
	in.close();
	return 0;
}