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
| #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std;
int n, m, t, s, e, tot, sub[205], f[205][205], g[205][205];
struct A {int s, t, len;} a[205];
void mul(int x[205][205], int y[205][205]) { int ans[205][205]; memset(ans, 0x3f, sizeof(ans)); for(int k=1; k<=m; k++) for(int i=1; i<=m; i++) for(int j=1; j<=m; j++) ans[i][j]=min(ans[i][j], x[i][k]+y[k][j]); memcpy(x, ans, sizeof(ans)); }
int main() { memset(f, 0x3f, sizeof(f)); scanf("%d%d%d%d", &n, &t, &s, &e); sub[++tot]=s; sub[++tot]=e; for(int i=1; i<=t; i++) { scanf("%d%d%d", &a[i].len, &a[i].s, &a[i].t); sub[++tot]=a[i].s; sub[++tot]=a[i].t; } sort(sub+1, sub+tot+1); m=unique(sub+1, sub+tot+1)-sub-1; for(int i=1; i<=t; i++) { int x=lower_bound(sub+1, sub+m+1, a[i].s)-sub; int y=lower_bound(sub+1, sub+m+1, a[i].t)-sub; f[x][y]=min(f[x][y], a[i].len); f[y][x]=min(f[y][x], a[i].len); } memcpy(g, f, sizeof(g)); n--; while(n) { if(n&1) mul(g, f); mul(f, f); n>>=1; } s=lower_bound(sub+1, sub+m+1, s)-sub; e=lower_bound(sub+1, sub+m+1, e)-sub; printf("%d\n", g[s][e]); }
|