Cod sursa(job #420455)

Utilizator s_holmesSherlock Holmes s_holmes Data 19 martie 2010 11:19:53
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <cstdio>
#include <vector>
#define NMAX 30001
using namespace std;
int N, M, X, Y;
int C[NMAX], NR = 0;
bool viz[NMAX];
int d[NMAX];
vector< pair<int,int> > V[NMAX];

void citire()
{
	int x, y, c;
	scanf("%d%d%d%d", &N, &M, &X, &Y);
	for(int i = 1 ; i <= M ; i++)
	{
		scanf("%d%d%d", &x, &y, &c);
		V[x].push_back(make_pair(y, c));
		V[y].push_back(make_pair(x, c));
	}
	
}

void BFS()
{
	vector< pair<int,int> > :: iterator it;
	
	C[++NR] = X;
	d[X] = 0;
	
	for(int i = 1 ; i <= NR ; i++)
		for(it = V[C[i]].begin() ; it != V[C[i]].end(); it++)
			if(!viz[it->first])
			{
				if(it->first < C[i])
					d[it->first] = d[C[i]] - it->second;
				else
					d[it->first] = d[C[i]] + it->second;
				
				if(it->first == Y)
					return;
				
				C[++NR] = it->first;
				viz[it->first] = 1;
			}
}

void scrie()
{
	vector< pair<int,int> > :: iterator it;
	for(int i = 1 ; i <= N ; i++)
	{
		for(it = V[i].begin() ; it != V[i].end(); it++)
			printf("%d [%d]  ",it->first, it->second);
		printf("\n");
	}
	
	for(int i = 1 ; i <= N ; i++)
		printf("%d ",d[i]);
	
}

int main()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	citire();
	BFS();
	//scrie();
	printf("%d",d[Y]);
	return 0;
}