Pagini recente » Cod sursa (job #1864400) | Cod sursa (job #1133438) | Cod sursa (job #3137970) | Cod sursa (job #372793) | Cod sursa (job #2084187)
#include <bits/stdc++.h>
using namespace std;
const int maxK=1e3;
const int maxN=25*1e4+4;
const int INF=0x3f3f3f3f;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch(){
++sp;
if(sp==4096){
sp=0;
fread(buff,1,4096,fin);
}
return buff[sp];
}
public:
InParser(const char* nume){
fin=fopen(nume,"r");
buff=new char[4096]();
sp=4095;
}
InParser& operator >> (int &n){
char c;
while(!isdigit(c=read_ch())&&c!= '-');
int sgn=1;
if(c=='-'){
n=0;
sgn=-1;
}
else n=c-'0';
while(isdigit(c=read_ch()))
n=10*n+c-'0';
n*=sgn;
return *this;
}
};
int dist[maxN];
queue<int> Que[maxK+4];
vector<pair<int,int> >v[maxN];
int n,x,y,m;
int main()
{
InParser f("pscnv.in");
freopen("pscnv.out","w",stdout);
f>>n>>m>>x>>y;
for(int i=1;i<=m;i++){
int x,y,z;
f>>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;
}