Cod sursa(job #1060287)

Utilizator pafcalUAIC-Alexandru-Pavaloi pafcal Data 17 decembrie 2013 20:20:23
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include<fstream>
#include <stdlib.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");

struct nod
{
	int val, dist;
	nod *succ;
};
nod *V[30001],*p;
long i;
int n,m,a,b,x,y,aux,final,viz[30001],d;


void sate(int x, int d)
{
	
	nod *p;
	if (viz[x]==0)
	{
		viz[x] = 1;
		if (x == b) { fout << d; final = 1; exit(0); }
		else
		{
			
			
			p = V[x];
			while (p != NULL)
			{
				if (viz[p->val] == 0)
				{
					
					//viz[p->val] = 1;
					if (x < p->val)
					{
						d += p->dist;
						sate(p->val, d);
						d -= p->dist;
					}
					else
					{
						d -= p->dist;
						sate(p->val, d);
						d += p->dist;
					}

				}
				p = p->succ;
			}

		}
	}
}



int main()
{
	fin >> n >> m >> a >> b;
	for (i = 1; i <= m; i++)
	{
		fin >> x >> y >> d;
		p = new nod;
		p->val = x;
		p->dist = d;
		p->succ = V[y];
		V[y] = p;
		p = new nod;
		p->val = y;
		p->dist = d;
		p->succ = V[x];
		V[x] = p;
	}

	if (a > b)
	{
		aux = a;
		a = b;
		b = aux;
	}
	final = 0;
	sate(a, 0);
	fin.close();
	fout.close();
	return 0;
}