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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream> #include <algorithm> #include <cstring> #include <map>
using namespace std;
const int N = 210, M = 1000010;
int n, m, K, S, E; int g[N][N]; int res[N][N];
void mul(int c[][N], int a[][N], int b[][N]) { static int temp[N][N]; memset(temp, 0x3f, sizeof temp); for (int k = 1; k <= n; k ++ ) for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) temp[i][j] = min(temp[i][j], a[i][k] + b[k][j]); memcpy(c, temp, sizeof temp); }
void qmi() { memset(res, 0x3f, sizeof res); for (int i = 1; i <= n; i ++ ) res[i][i] = 0; while (K) { if (K & 1) mul(res, res, g); mul(g, g, g); K >>= 1; } }
int main() { cin >> K >> m >> S >> E; map<int, int> ids; if (!ids.count(S)) ids[S] = ++ n; if (!ids.count(E)) ids[E] = ++ n; S = ids[S], E = ids[E]; memset(g, 0x3f, sizeof g); while (m -- ) { int a, b, c; cin >> c >> a >> b; if (!ids.count(a)) ids[a] = ++ n; if (!ids.count(b)) ids[b] = ++ n; a = ids[a], b = ids[b]; g[a][b] = g[b][a] = min(g[a][b], c); } qmi(); cout << res[S][E] << endl; return 0; }
|