Cod sursa(job #184108)

Utilizator andrei_h5n1Haidau Andrei andrei_h5n1 Data 23 aprilie 2008 01:18:19
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define dim 30010

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

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

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 df(int x)
{
	if(x==X)
	{
		if(sum<0)
			sum*=-1;

		printf("%ld\n", sum);

		exit(0);
	}
	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;
		}
	}
}
int main()
{

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

	int x, y; long c;

	scanf("%d %ld %d %d", &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);
	}
	df(Y);

	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;
	}
} */