Cod sursa(job #639851)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 24 noiembrie 2011 01:08:21
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <cstdio>
#include <cstring>

#define file_in "sate.in"
#define file_out "sate.out"

#define nmax 150100

int N,M,X,Y;
int a[nmax];
int b[nmax];
int c[nmax];
int d[nmax];
int i;

int main(){
	
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d %d %d %d", &N,&M,&X,&Y);
	for (i=1;i<=M;++i){
		scanf("%d %d %d", &a[i], &b[i], &c[i]);
		
		if (a[i]==X)
			d[b[i]]=c[i];
		if (b[i]==X)
			d[a[i]]=-c[i];
	}
	
	for (i=1;i<=N;++i)
		 if (i!=X)
			  if (d[i]==0)
				  d[i]=0x3f3f3f3f;
			  
	int ok=0;
	while(!ok){
		ok=1;
		for (i=1;i<=M;++i){
			if (d[a[i]]>d[b[i]]-c[i]){
				d[a[i]]=d[b[i]]-c[i];
				ok=0;
			}
			if (d[b[i]]>d[a[i]]+c[i]){
				d[b[i]]=d[a[i]]+c[i];
				ok=0;
			}
		}
	}
	
	printf("%d\n", d[Y]);
	
	return 0;
	
}