Cod sursa(job #183387)

Utilizator andrei_h5n1Haidau Andrei andrei_h5n1 Data 22 aprilie 2008 00:43:16
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <stdio.h>

using namespace std;

#define dim 30010

struct nod
{
	int inf;
	long cost;
	nod *next;
};
nod *l[dim], *in, *sf;

int s[dim];
long gasit, n, m, sum, i, X, Y;

void add(int x, int y, long c)
{
	if(x>y)
		c*=-1;

	nod *p=new nod;
	p->inf=y;
	p->cost=c;
	p->next=l[x];
	l[x]=p;

}
void bf(int );
int main()
{

	freopen("file.in", "r", stdin);
	freopen("file.out", "w", stdout);

	int x, y; long c;

	scanf("%ld %ld %ld %ld", &n, &m, &X, &Y);

	for(i=1; i<=m; i++)
	{
		scanf("%d %d %ld", &x, &y, &c);
		add(x, y, c);
		add(y, x, c);
	}
	bf(X);

	return 0;
}
void bf(int x)
{
	int ok=0;

	in=new nod;
	in->inf=x;
	in->cost=0;
	in->next=0;
	sf=in;

	s[x]=1;

	while(!ok)
	{
		nod *p=l[in->inf];
		int ind;
		while(p)
		{
			ind=p->inf;
			if(!s[ind])
			{
				nod *aux=new nod;

				aux->inf=ind;
				aux->cost=in->cost+p->cost;
				aux->next=0;
				sf->next=aux;
				sf=sf->next;

				s[ind]=1;

				if(ind==Y)
				{
					printf("%ld\n", sf->cost);
					ok=1;
					break;
				}
			}
			p=p->next;
		}
		nod *aux1;
		aux1=in;
		in=in->next;
		delete aux1;
	}
}
/*void df(int x)
{
	if(!gasit)
	{
		if(x==Y)
		{
			gasit=1;
			if(sum<0)
				sum*=-1;

			printf("%ld\n", sum);
		}
		else
		{
			int ind;
			nod *p=l[x];
			while(p)
			{
				ind=p->inf;
				if(!s[ind])
				{
					s[ind]=1;
					sum+=p->cost;
					df(ind);
					s[ind]=0;
					sum-=p->cost;
				}
				p=p->next;
			}
		}
	}
} */