Pagini recente » Cod sursa (job #424912) | Cod sursa (job #1042340) | Cod sursa (job #678158) | Cod sursa (job #1553247) | Cod sursa (job #998692)
Cod sursa(job #998692)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
const int NMAX = 250005, INF = 0x3f3f3f3f, MAXB = 8192;
int N, M, Start, End, X, Y, C, Ans[NMAX], Ptr;
vector<pair<int, int> > G[NMAX];
vector<int> List[1010];
char Buf[MAXB];
int GetAns()
{
memset(Ans, INF, sizeof(Ans));
Ans[Start] = 0;
List[0].push_back(Start);
for(int i = 0; i <= 1000; ++ i)
for(size_t j = 0; j < List[i].size(); ++ j)
{
int Node = List[i][j];
if(Node == End) return i;
if(Ans[Node] != i) continue;
for(size_t k = 0; k < G[Node].size(); ++ k)
{
int NowCost = max(Ans[Node], G[Node][k].second);
if(NowCost >= Ans[ G[Node][k].first ]) continue;
Ans[ G[Node][k].first ] = NowCost;
List[NowCost].push_back( G[Node][k].first );
}
}
}
int GetInt()
{
int Ans = 0;
while(!isdigit(Buf[Ptr]))
if(++ Ptr >= MAXB)
fread(Buf, 1, MAXB, stdin), Ptr = 0;
while(isdigit(Buf[Ptr]))
{
Ans = Ans * 10 + Buf[Ptr] - '0';
if(++ Ptr >= MAXB)
fread(Buf, 1, MAXB, stdin), Ptr = 0;
}
return Ans;
}
int main()
{
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
fread(Buf, 1, MAXB, stdin);
N = GetInt();
M = GetInt();
Start = GetInt();
End = GetInt();
for(int i = 1; i <= M; ++ i)
{
X = GetInt();
Y = GetInt();
C = GetInt();
G[X].push_back(make_pair(Y, C));
G[Y].push_back(make_pair(X, C));
}
printf("%i\n", GetAns());
return 0;
}