Cod sursa(job #482688)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 4 septembrie 2010 16:17:06
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>
#include <vector>
#include <queue>
#define Nmax 250002
#define Valmax 1002
#define pb push_back
#define mp make_pair
#define y first
#define c second

using namespace std;

vector< pair<int,int> > G[Nmax];
queue< int > v[Valmax];
int d[Nmax];
int N,M,X,Y,vmax;

inline int Maxim(int x,int y){ return x>y ? x:y; }

void dijkstra(){
	vector< int >:: iterator it;
	vector< pair<int,int> >:: iterator it2;
	int i,wh;
	for(i=1;i<=N;++i)
		d[i]=vmax+1;
	d[X]=0;
	v[0].push(X);
	
	for(i=0; i<=vmax; ++i)
		while( ! v[i].empty() ){
				wh=v[i].front(); v[i].pop();
				if( wh==Y ) return;
				if( d[wh] == i ){
					for(it2=G[wh].begin(); it2!=G[wh].end(); ++it2)
						if( d[it2->y] > Maxim(d[wh],it2->c) ){
							d[it2->y] = Maxim(d[wh],it2->c);
							v[d[it2->y]].push(it2->y);
						}
				}
		}
}

int main(){
	int i,x,y,z;
	freopen("pscnv.in","r",stdin);
	freopen("pscnv.out","w",stdout);
	scanf("%d%d%d%d",&N,&M,&X,&Y);
	for(i=1;i<=M;++i){
		scanf("%d%d%d",&x,&y,&z);
		if( x!=y ){
			G[x].pb(mp(y,z));
			vmax=Maxim(vmax,z);
		}
	}
	
	dijkstra();
	
	printf("%d\n",d[Y]);
	fclose(stdin); fclose(stdout);
	return 0;
}