1 /**************************************************************
2 Problem: 1706
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:192 ms
7 Memory:2152 kb
8 ****************************************************************/
9
10 //BZOJ 1706
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=201,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int n,m,s,t,cnt,x[N],y[N],len[N],c[N];
32 struct Matrix{
33 int v[N][N];
34 int* operator [] (int x) {return v[x];}
35 Matrix(int x=0){memset(v,x,sizeof v);}
36 }map(0x3f),dis(0x3f),ans(0x3f),tmp(0x3f);
37
38 void floyd(Matrix &a,Matrix &b,Matrix &c){
39 F(k,1,cnt) F(i,1,cnt) F(j,1,cnt)
40 if (c[i][j]>a[i][k]+b[k][j])
41 c[i][j]=a[i][k]+b[k][j];
42 }
43 void solve(int b){
44 F(i,1,cnt) dis[i][i]=0;
45 for(;b;b>>=1){
46 if (b&1){
47 ans=Matrix(0x3f);
48 floyd(map,dis,ans);
49 dis=ans;
50 }
51 floyd(map,map,tmp);
52 map=tmp;
53 tmp=Matrix(0x3f);
54 }
55 }
56 int main(){
57 #ifndef ONLINE_JUDGE
58 freopen("1706.in","r",stdin);
59 freopen("1706.out","w",stdout);
60 #endif
61 n=getint(); m=getint(); s=getint(); t=getint();
62 F(i,1,m){
63 len[i]=getint(); x[i]=getint(); y[i]=getint();
64 c[++cnt]=x[i]; c[++cnt]=y[i];
65 }
66 sort(c+1,c+cnt+1);
67 cnt=unique(c+1,c+cnt+1)-c-1;
68 F(i,1,m){
69 x[i]=lower_bound(c+1,c+cnt+1,x[i])-c;
70 y[i]=lower_bound(c+1,c+cnt+1,y[i])-c;
71 map[x[i]][y[i]]=map[y[i]][x[i]]=min(map[x[i]][y[i]],len[i]);
72 }
73 s=lower_bound(c+1,c+cnt+1,s)-c;
74 t=lower_bound(c+1,c+cnt+1,t)-c;
75 solve(n);
76 printf("%d\n",ans[s][t]);
77 return 0;
78 }