Cod sursa(job #786410)

Utilizator danalex97Dan H Alexandru danalex97 Data 11 septembrie 2012 12:39:48
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

const int Nmax = 250010;
const int oo = 1<<30;
const int Pars = 100;

vector< pair<int,int> > Leg[Nmax];
int N,M,Start,End;

int D[Nmax];
vector< int > W[Nmax];

ifstream F("pscnv.in");
ofstream G("pscnv.out");

int P; char Str[Pars];
inline int Get() 
{
	int Rez = 0;
	while (Str[P]>='0' && Str[P] <= '9') 
		Rez = Rez * 10 + Str[P++] - '0'; ++P;
	return Rez;
}

int main()
{
	F>>N>>M>>Start>>End;
	F.get();
	for (int i=1;i<=M;++i)
	{
		F.getline(Str,Pars); P=0;
		int x=Get(),y=Get(),c=Get();
		Leg[x].push_back( make_pair(y,c) );
	}
	for (int i=1;i<=N;++i) D[i]=oo;
	
	W[0].push_back(Start);
	D[Start]=0;
	
	for (int c=0; c<1001; c++)
        for (int i=0; i<int(W[c].size()); i++)
        {
            int Nod=W[c][i];
            if ( Nod==End ) 
			{
				G<<c<<'\n';
				return 0;
			}
			
            if (D[Nod]!=c)  continue;
			
            for (int j=0; j<int(Leg[Nod].size()); j++)
            {
                int cost=max(Leg[Nod][j].second,c);
                if ( cost>=D[Leg[Nod][j].first] ) continue;
                D[Leg[Nod][j].first]=cost;
                W[cost].push_back(Leg[Nod][j].first);
            }
        }
}