Cod sursa(job #628906)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 2 noiembrie 2011 12:16:55
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>

#define max_n 30001
#define nod first
#define cost second

using namespace std;

int n,m,i,x,y,z,d1,s;
vector < pair < int,int > > g[max_n];
vector < pair < int,int > >::iterator it;
int q[max_n];
int d[max_n];
bool ok[max_n];

int BF() {
	int st,dr;
	st=dr=1;
	q[1]=s;
	d[s]=0;
	ok[s]=true;
	
	memset(ok,false,sizeof(ok));
	for (;st<=dr; st++) {
        x=q[st];		
		for (it=g[x].begin(); it!=g[x].end(); it++) {
			y=(*it).nod;
			if (!ok[y]) {
				ok[y]=true;
				q[++dr]=y;
				d[y]=d[x]+(*it).cost;
			}
			if (y==d1) return d[y];
		}
	}
}
	

int main () {
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	scanf("%d%d%d%d",&n,&m,&s,&d1);
	for (i=1; i<=m; i++) {
		scanf("%d%d%d",&x,&y,&z);
		g[x].push_back(make_pair(y,z));
		g[y].push_back(make_pair(x,-z));
	}
	
	printf("%d\n",BF());
	return 0;
}