Cod sursa(job #133446)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 8 februarie 2008 18:07:10
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <stdio.h>
#include <stdlib.h>
int n, m, x, y, viz[30000];

typedef struct nod
{
	int x, d;
	nod *a;
} *pNod;

pNod v[30001];

void citire()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	int i, a, b, d;
	pNod p;

	scanf("%d %d %d %d",&n,&m,&x,&y);
	for (i = 1; i <= m; i++)
	{
		scanf("%d %d %d",&a, &b, &d);
		p = new nod;
		p -> x = a;
		p -> d = d;
		p -> a = v[b];
		v[b] = p;
		p = new nod;
		p -> x = b;
		p -> d = d;
		p -> a = v[a];
		v[a] = p;
	}
}

void DF(int nod, int dist)
{
	pNod p;
	if (nod == y)
	{
		printf("%d\n",dist);
		exit(0);
	}
	viz[nod] = 1;

	for (p = v[nod]; p != NULL; p = p -> a)
	{
		if (!viz[p->x])
		if (p -> x < nod)
		{
			dist -= p -> d;
			DF(p -> x, dist);
			dist += p -> d;
		}
		else 
		{
			dist += p -> d;
			DF(p -> x, dist);
			dist -= p -> d;
		}
	}
}

int main()
{
	citire();
	if (x > y)
	{
		int aux = x;
		x = y;
		y = aux;
	}
	DF(x,0);
	return 0;
}