Cod sursa(job #751184)

Utilizator BoSs_De_BosSSeFu SeFiLoR BoSs_De_BosS Data 24 mai 2012 19:34:00
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

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

const int N = 250010;

int n, m, x, y, sol[N];
vector<pair<int, int> > v[N];
vector<int> l[1010];

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

int main() {
	int i, a, b, c, nod, j, cost, f;
	vector<pair<int, int> >::iterator it;
	
	in >> n >> m >> x >> y;
	
	for(i = 1; i<=n; ++i) {
		in >> a >> b >> c;
		
		v[a].push_back(pair<int, int>(b, c));
	}
	
	for(i = 1; i<=n; ++i)
		sol[i] = 1010;
	
	sol[x] = 0;
	l[0].push_back(x);
	
	for(i = 0; i<=1000; ++i)
		for(j = 0; j != l[i].size(); ++j) {
			nod = l[i][j];
			
			if(nod == y) {
				out << i << "\n";
				return 0;
			}
			
			if(sol[nod]!=i)
				continue;
			
			for(it = v[nod].begin(); it!=v[nod].end(); ++it) {
				cost = max(it->second, i);
				f = it->first;
				
				if(sol[f] > cost) {
					sol[f] = cost;
					l[cost].push_back(f);
				}
			}
		}
	
	return 0;
}