Cod sursa(job #2366609)

Utilizator Hey_HeyIacovlev Denis Hey_Hey Data 4 martie 2019 21:11:06
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include<iostream>
#include<fstream>

using namespace std;

ifstream fi("sate.in");
ofstream fo("sate.out");

typedef
 struct node
 	{
 		int date, dist;
 		node *next;
	} *lista;
	
bool viz[30010], b=1;
int N, M, X, Y, i, j, k, l, x, y, d, dist;
lista Lda[30005], head, temp, tail;

void dfs(int z)
{
	if(b)
	{
		//fo << " 1 \n";
		viz[z]=1;
		for(lista head = Lda[z]; head; head=head->next)
		if(!viz[head->date])
		{
			//fo << " 2 \n";
			if(z < head->date) dist+=head->dist; else dist-=head->dist;
			int w=dist;
			if(head->date!=Y)
			{
			//	fo << " 3 \n";
				dfs(head->date);
				dist=w;
			}
			else
			{
			//	fo << " 4 \n"; 
				b=0;
				d=dist;
			}
		}
	}
}

void add(lista &head, int q, int w)
{
	lista temp=new node;
	temp->date=q;
	temp->dist=w;
	temp->next=head;
	head=temp;
}

int main()
{
	fi >> N >> M >> X >> Y;
	
	for(i=1; i<=N; i++)
	{
		Lda[i]=NULL;
	}

	for(i=1; i<=M; i++)
	{
		fi >> x >> y >> d;
		
		if(Y==x) add(Lda[y],x,d);
		else if(Y==y) add(Lda[x],y,d);
		else 
		{
			add(Lda[y],x,d);
			add(Lda[x],y,d);
		}
		
		
		
	}
	
	dfs(X);
	
	fo << d;	
}