Pagini recente » Cod sursa (job #156846) | Cod sursa (job #388071) | Cod sursa (job #2756695) | Cod sursa (job #437576) | Cod sursa (job #2077123)
#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <fstream>
#include <algorithm>
#define KMax 1000
#define NMax 250000
using namespace std;
ifstream f("pscnv.in");
ofstream g("pscnv.out");
vector < pair < int, int > > graf[NMax + 5];
queue < int > mylist[KMax + 1];
int n, m, x, y;
void Solve()
{
vector < int > best(n + 1, -1);
mylist[0].push(x);
for(int i = 0; i <= KMax; ++i)
{
if(mylist[i].empty()) continue;
int nod;
while(!mylist[i].empty())
{
nod = mylist[i].front();
mylist[i].pop();
if(nod == y)
{
g << i << '\n';
return;
}
if(best[nod] != -1) continue;
best[nod] = i;
for(vector < pair < int, int > >::iterator edge = graf[nod].begin(); edge != graf[nod].end(); ++edge)
{
if(best[edge -> first] == -1)
{
mylist[max(edge -> second, i)].push(edge -> first);
}
}
}
}
}
int main()
{
int a, b, c;
f >> n >> m >> x >> y;
for(int i = 1; i <= m; ++i)
{
f >> a >> b >> c;
graf[a].push_back(make_pair(b, c));
}
Solve();
}