Cod sursa(job #360812)

Utilizator iulia609fara nume iulia609 Data 2 noiembrie 2009 04:25:06
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<stdio.h>
#include<vector>
#define NMAX 30001
using namespace std;

int N, M, V[NMAX];
short int X, Y, C[NMAX];
vector<short int> A[NMAX];
vector<int> B[NMAX];
char S[30];

int main()
{  int t, aux,k, in, sf, cost,x,y,i,l;

	freopen("sate.in", "r", stdin);
	freopen("sate.out", "w", stdout);
	
	scanf("%d%d%hd%hd", &N, &M, &X, &Y);
	
	for(i = 1; i <= M; i++)
		{
			//scanf("%d%d%d", &x, &y, &cost);
			
			gets(S);
			x = y = cost = 0;
			
			for(t = 0; S[t] != ' '; t++)
				x = x*10 + S[t]-'0';
			//t++;
			for(++t; S[t] !=' '; t++)
				y = y*10 + S[t]-'0';
			//t++;
			for(++t; S[t] !=' '; t++)
				cost = cost* 10 + S[t]-'0';
			
			A[x].push_back(y);
			A[y].push_back(x);
			B[x].push_back(cost);
			B[y].push_back(cost);
		}
	
	
	in = sf = 1;
	C[1] = X;
	while(!V[Y])
	{
		X = C[in++];
		
		//l = A[X].size();
		k = 0;
	
		while(k < A[X].size())
		{
			aux = A[X][k];
			if(!V[aux])
			{
				t = B[X][k];
				V[aux] = V[X];
				if(X < aux)
					V[aux] += t;
				  else
					V[aux] -= t;
				
				sf++;
				C[sf] = aux;
			}
			k++;
		}
		//in++;
	}
 
	printf("%d\n",V[Y]);
	return 0;
	
}