Pagini recente » Cod sursa (job #255989) | Cod sursa (job #1654046) | Cod sursa (job #2049259) | Cod sursa (job #371058) | Cod sursa (job #712447)
Cod sursa(job #712447)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define maxn 250010
#define maxk 1010
#define maxd 55
int n, m, k, pz, cost, fiu, nod, a, b, c, x, y, sol[maxn];
vector<int> l[maxk];
vector<pair<int, int> > v[maxn];
char buf[maxd];
int getNr()
{
int nr=0;
while(buf[pz]>='0' && buf[pz]<='9')
{
nr=nr*10+buf[pz]-'0';
++pz;
}
++pz;
return nr;
}
int main()
{
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
scanf("%d%d%d%d\n", &n, &m, &x, &y);
for(int i=1; i<=m; ++i)
{
fgets(buf, maxd, stdin);
pz=0;
a=getNr();
b=getNr();
c=getNr();
v[a].push_back(make_pair(b, c));
}
for(int i=1; i<=n; ++i)
sol[i]=maxk;
sol[x]=0;
l[0].push_back(x);
for(int i=0; i<=1000; ++i)
{
for(int pz=0; pz<l[i].size(); ++pz)
{
nod=l[i][pz];
if(nod==y)
{
printf("%d\n", i);
return 0;
}
if(sol[nod]!=i)
continue;
for(vector<pair<int, int> > :: iterator it = v[nod].begin(); it!=v[nod].end(); ++it)
{
fiu=it->first;
cost=max(i, it->second);
if(sol[fiu]>cost)
{
sol[fiu]=cost;
l[cost].push_back(fiu);
}
}
}
}
return 0;
}