Cod sursa(job #2366973)

Utilizator Hey_HeyIacovlev Denis Hey_Hey Data 4 martie 2019 23:27:33
Problema Sate Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 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[30009], b=1;
int N, M, X, Y, i, x, y, d, dist;
lista Lda[30009];

void dfs(int z, int w)
{
	if(z==Y)
	{
		b=0;
		fo << w << '\n';
		return;
	}
		viz[z]=1;
	if(b)
	{
		//fo << " 1 \n";
		
		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;
			//	fo << " 3 \n";
			dfs(head->date, head->dist+w);
				
		}
	}
}

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;
		
			add(Lda[y],x,-d);
			add(Lda[x],y,d);
		
	}
	
	/*fo << "\n\n Listele de adicenta: \n";
	for(i=1; i<=N; i++)
	{
		temp=Lda[i];
		fo << " Lda[" << i << "] : ";
	 	while(temp)
	 	{
	 		
	 		fo << temp->date << " -> ";	
	 		temp=temp->next;
		}
		fo << "NULL\n";
		
		
	}*/
	
	dfs(X, 0);
		
}