Pagini recente » Cod sursa (job #2707219) | Cod sursa (job #192454) | Cod sursa (job #613691) | Cod sursa (job #858012) | Cod sursa (job #2084179)
#include <bits/stdc++.h>
using namespace std;
const int maxK=1e3;
const int maxN=25*1e4+4;
const int INF=0x3f3f3f3f;
int dist[maxN];
queue<int> Que[maxK+4];
vector<pair<int,int> >v[maxN];
int n,x,y,m;
int main()
{
freopen("pscnv.in","r",stdin);
freopen("pscnv.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&x,&y);
for(int i=1;i<=m;i++){
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
v[x].push_back(make_pair(y,z));
}
for(int i=0;i<=n;i++)
dist[i]=INF;
Que[0].push(x);
int cost=0;
while(cost<=maxK){
if(Que[cost].empty()){
cost++;
continue;
}
int nod=Que[cost].front();
Que[cost].pop();
if(dist[nod]==INF){
if(nod==y){
printf("%d",cost);
return 0;
}
dist[nod]=cost;
for(auto it:v[nod])
if(dist[it.first]==INF)
Que[max(cost,it.second)].push(it.first);
}
}
printf("-1");
return 0;
}