Cod sursa(job #604543)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 23 iulie 2011 12:37:16
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include<stdio.h>
#include<vector>
#include<string>

#define maxN 250005
#define maxK 1005

using namespace std;

FILE*f=fopen("pscnv.in","r");
FILE*g=fopen("pscnv.out","w");

int n,m,x,y,R,a,b,c,l,i,j;
int nod,nodvcn;
bool second; int D[maxN]; char line[30];

vector< pair<int,short int> >G[maxN];
vector<int>List[maxK];

inline void citire () {
	
	fscanf(f,"%d %d %d %d\n",&n,&m,&x,&y);
	
	for ( i = 1 ; i <= m ; ++i ){
		fgets(line+1,20,f); R = 0; second = 0; l = strlen(line+1);
		for ( j = 1 ; line[j] != '\n' && j <= l ; ++j ){
			if ( line[j] >= '0' && line[j] <= '9' )
				R = R * 10 + line[j] - '0';
			else{
				if ( line[j] == ' ' ){
					if ( second )
						b = R;
					else
						a = R,second = 1;
					R = 0;
				}
			}
		}
		c = R;
		G[a].push_back( make_pair(b,c) );
	}
}

inline short int max ( short  int a, short int b ){
	return a > b ? a : b;
}

inline void dijkstra () {
	
	for ( i = 1 ; i <= n ; ++i ){
		D[i] = maxK;
	}		
	D[x] = 0;
	vector< pair<int,short int> >::iterator itt;
	List[0].push_back(x);
	
	for ( i = 0 ; i <= 1000 ; ++i ){
		
		for ( j = 0 ; j < List[i].size() ; ++j ){
			nod = List[i][j];
			if ( D[nod] != i )	continue ;
			if ( nod == y )	break ;
			
			for ( itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){
				nodvcn = itt->first; c = itt->second;
				if ( D[nodvcn] > max(D[nod],c) ){
					D[nodvcn] = max(D[nod],c);
					List[ D[nodvcn] ].push_back(nodvcn);
				}
			}
		}
	}
	
	fprintf(g,"%d\n",D[y]);
}

int main () {
	
	citire();
	dijkstra();
	
	fclose(f);
	fclose(g);
	
	return 0;
}