Cod sursa(job #343238)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 25 august 2009 12:32:48
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<cstdio>
#define N 30001
#define M 100025
#include<vector>
using namespace std;
int m,d[N];
short int n,x1,y1;
vector <short int>a[N],a1[N];
bool b[N];
void citire()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	scanf("%hd%d%hd%hd",&n,&m,&x1,&y1);
	short int f,g,c;
	for (int i=1; i<=m; ++i)
	{
		scanf("%hd%hd%hd",&f,&g,&c);
		a[f].push_back(g);
		a[g].push_back(f);
		a1[f].push_back(c);
		a1[g].push_back(-c);
	}
}
void bfs()
{
	int coada[N],u=0,p=0,x,y;
	coada[u++]=x1;
	b[x1]=true;
	while (u!=p)
	{
		x=coada[p++];
		int g=a[x].size();
		for (int i=0; i<g; ++i)
		{
			y=a[x][i];
			if (!b[y])
			{
				b[y]=true;
				coada[u++]=y;
				d[y]=a1[x][i]+d[x];
			}
			if(y==y1)
				return;
		}
	}
}
int main()
{
	citire();
	bfs();
	printf("%d",d[y1]);
	return 0;
}