1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include<bits/stdc++.h> #define RG register #define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout); #define clear(x, y) memset(x, y, sizeof(x)); using namespace std;
template<typename T = int> inline T read() { T data=0, w=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1, ch=getchar(); while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar(); return data*w; }
const int N(210); typedef long long ll; int k, t, s, e, cnt, pos[2010]; class Matrix { private: ll a[N][N]; public: Matrix() { clear(a, 0x3f); } ll *operator [] (int index) { return a[index]; } Matrix operator * (Matrix &b) { Matrix c; for(RG int k=1;k<=cnt;k++) for(RG int i=1;i<=cnt;i++) for(RG int j=1;j<=cnt;j++) c[i][j]=min(c[i][j], a[i][k]+b[k][j]); return c; } }G, A;
ll x, y, z; int main() { k=read(); t=read(); s=read(); e=read(); for(RG int i=1;i<=t;i++) { z=read(); x=read(); y=read(); if(!pos[x]) pos[x]=++cnt; if(!pos[y]) pos[y]=++cnt; G[pos[x]][pos[y]]=G[pos[y]][pos[x]]=min(G[pos[x]][pos[y]], z); } for(RG int i=1;i<=cnt;i++) A[i][i]=0; while(k) { if(k&1) A=A*G; G=G*G; k>>=1; } printf("%lld\n", A[pos[s]][pos[e]]); return 0; }
|