
思路
变种Flyod+快速幂思想(倍增),还需要提前做离散化。
AC code
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 <iostream> #include <cstring> #include <algorithm> #include <map>
using namespace std;
const int N = 210; int g[N][N]; int res[N][N]; int k, n, m, s, e;
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; memset(g, 0x3f, sizeof(g)); map<int, int> mp; if(!mp.count(s)) mp[s] = ++n; if(!mp.count(e)) mp[e] = ++n; s = mp[s], e = mp[e]; while (m -- ){ int a, b, c; cin >> c >> a >> b; if(!mp.count(a)) mp[a] = ++n; if(!mp.count(b)) mp[b] = ++n; a = mp[a], b = mp[b]; g[a][b] = g[b][a] = min(g[a][b], c); } qmi(); cout << res[s][e] << endl; return 0; }
|