Cod sursa(job #356919)

Utilizator prisonbreakMichael Scofield prisonbreak Data 17 octombrie 2009 16:16:11
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <cstdio>
#include <vector>

using namespace std;

#define DIM 30002

void read();
void bfs();
vector <int>G[DIM];
vector <long>Cost[DIM];
int i, x, y, X, Y, d[DIM],p, u, viz[DIM], c[DIM], N, M;
long z;


int main()
{
	freopen ("sate.in","r",stdin);
	freopen ("sate.out","w",stdout);
	read();
	bfs();
	
	return 0;
}
void read()
{
	scanf ("%d%d%d%d\n",&N,&M,&X,&Y);
	for (i=1; i<=M; i++)
	{
		scanf ("%d%d%d\n",&x,&y,&z);
		G[x].push_back(y);
		G[y].push_back(x);
		Cost[x].push_back(z);
		Cost[y].push_back(z);
	}
}
void bfs()
{
	viz[X]=1;
	p=u=0;
	c[p]=X;
	while ( p<=u && !viz[Y])
	{
		x=c[p++];
		for (i=0; i<G[x].size(); i++)
		{
			if (!viz[G[x][i]])
			{   
				if (G[x][i] < x)
				d[G[x][i]]+=(d[x]-Cost[x][i]);
				else d[G[x][i]]+=d[x]+Cost[x][i];
				viz[G[x][i]]=1;
				c[++u]=G[x][i];
			}
		}
	}
printf ("%d\n",d[Y]);
}