1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5
6 const int inf = 1e9;
7 int Poi = 1,poi,Chart[300],chart[3000000],s,t,n,k,a,b,c;
8
9 struct MAT{
10 int mat[300][300];
11 MAT(){
12 for(int i = 1;i < 300;i++)
13 for(int j = 1;j < 300;j++)
14 mat[i][j] = inf;
15 }
16
17 void print(){cout << endl;
18 for(int i = 1;i <= n;i++){
19 for(int j = 1;j <= n;j++)
20 cout << mat[i][j] << ‘ ‘;
21 cout << endl;
22 }
23 }
24 }S;
25
26 struct list{
27 int a,b,c;
28 }arr[300];
29
30 int find(int x){
31 int L = 1,R = Poi-1,mid;
32 while(L < R){
33 mid = (L+R)/2;
34 if(Chart[mid] < x) L = mid+1;
35 else R = mid;
36 }return L;
37 }
38
39 void Init(){
40 sort(chart,chart+poi);
41 Chart[Poi++] = chart[0];
42 Poi = 1;
43 for(int i = 1;i < poi;i++)
44 if(chart[i] != Chart[Poi-1]) Chart[Poi++] = chart[i];
45 s = find(s);
46 t = find(t);
47 for(int i = 1;i <= n;i++){
48 int aa,bb;
49 aa = find(arr[i].a),bb = find(arr[i].b),c = arr[i].c;
50 // printf("%d %d %d\n",aa,bb,c);
51 S.mat[aa][bb] = S.mat[bb][aa] = c;
52 }n = Poi-1;
53 }
54
55 MAT mul(MAT A,MAT B){
56 MAT C;
57 for(int k = 1;k <= n;k++)
58 for(int i = 1;i <= n;i++)
59 for(int j = 1;j <= n;j++)
60 C.mat[i][j] = min(C.mat[i][j],A.mat[i][k]+B.mat[k][j]);
61 return C;
62 }
63
64 MAT ksm(MAT A,int kk){
65 // printf("BFYAYIYE");
66 MAT B = A; kk--;
67 // if(!kk) return A; 显然这行代码没有任何问题,但是加入后这个RE了,求大佬解释啊qwq
68
69
70 while(kk){
71 // B.print();
72 if(kk&1) B = mul(B,A);
73 A = mul(A,A);
74 kk >>= 1;
75 }return B;
76 }
77
78 int main(){
79 freopen("LLQ.in","r",stdin);
80 // freopen("LLQ.out","w",stdout);
81
82 scanf("%d%d%d%d",&k,&n,&s,&t);
83
84 for(int i = 1;i <= n;i++){
85 scanf("%d%d%d",&c,&a,&b);
86 arr[i].a = a;
87 arr[i].b = b;
88 arr[i].c = c;
89 // chart[poi++] = a;
90 chart[poi++] = a;
91 chart[poi++] = b;
92 }
93
94 Init();
95
96 // for(int i = 0;i <= Poi;i++) cout << Chart[i] << ‘ ‘;
97
98 // S.print();
99 S = ksm(S,k);
100
101 printf("%d",S.mat[s][t]);
102
103 return 0;
104 }