Cod sursa(job #140199)

Utilizator webspiderDumitru Bogdan webspider Data 21 februarie 2008 15:22:05
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 30001;

int D[ maxN ];
vector<int> q;
vector< pair<int,int> > much[ maxN ];
int N, M, X, Y;
int cr = 0;
int nodC, nodN, cost;

int main()
{
	int x,y,c;
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	
	scanf("%d %d %d %d\n", &N, &M, &X, &Y);
	for ( int i = 1; i <= M; i++ ) {
		scanf("%d %d %d\n", &x, &y, &c);
		much[x].push_back( make_pair( y,c ));
		much[y].push_back( make_pair( x,c ));
	}
	q.push_back( X );
	
	for ( int i = 0; i < q.size(); i++ ) {
		nodC = q[ i ];
		for ( int j = 0; j < much[ nodC ].size(); j++ )
			if ( !D[ much[nodC][j].first ] ) {
				nodN = much[nodC][j].first;
				cost = much[nodC][j].second;
				if ( nodN > nodC ) D[ nodN ] = D[ nodC ] + cost;
				else D[ nodN ] = D[ nodC ] - cost;
				q.push_back( nodN );
			}
	}
	
	printf("%d\n", D[Y]);
	
	return 0;
}