Cod sursa(job #632656)

Utilizator balakraz94abcd efgh balakraz94 Data 11 noiembrie 2011 22:06:09
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<cstdio>
#include<vector>
#include<queue>
#define infile "sate.in"
#define outfile "sate.out"
#define n_max 30005
#define pb push_back
#define mkp make_pair
#define INF 20000005
#define FOR(g,it) \
   for(vector< pair< int, int > > ::iterator it = g.begin(); it!=g.end(); ++it)
using namespace std;

vector < pair < int, int > >  v[n_max];

vector < int > d(n_max, INF);

int n, x0, x1;

void citeste()
{
	freopen(infile,"r",stdin);
	
	int m, x, y, c;
	
	scanf("%d %d %d %d",&n, &m, &x0, &x1);
	
	while(m--)
	{
		scanf("%d %d %d",&x,&y,&c);
		
		v[x].pb(mkp(y,c));
		v[y].pb(mkp(x,-c));
	}
	
	fclose(stdin);
}


void bfs()
{
	queue < int > q;
	
	q.push(x0);
	d[x0] = 0;
	
	while(!q.empty() && d[x1]==INF)
	{
		int x = q.front();
		q.pop();
		
		FOR(v[x],it)
			if(d[it->first]==INF)
			{
				d[it->first] = d[x] + it->second; 
				q.push(it->first);
			}
	}
}


void afiseaza()
{
	freopen(outfile,"w",stdout);

	printf("%d\n",d[x1]);
	
	fclose(stdout);
}


int main()
{
	citeste();
	bfs();
	afiseaza();
	
	return 0;
}