真假Floyd : 倍增

https://www.acwing.com/problem/content/347

\(求从起点S到终点E恰好经过N条边(可以重复经过)的最短路.\)

\(即求出原距离矩阵的N次方,可以用类似qmi()的方式优化.\)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 110;
int k, n, m, S, E;
int g[N][N];
int res[N][N];

void mul(int c[][N], int a[][N], int b[][N]) {
    static int tmp[N][N];
    memset(tmp, 0x3f, sizeof tmp);
    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                tmp[i][j] = min(tmp[i][j], a[i][k] + b[k][j]);
    memcpy(c, tmp, sizeof tmp);
}

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;
    }
}

/* res[i][i]就是在快速幂算法中先把res设置成单位元。
整数中的单位元是1,本题中的单位元就是对角线是0,其余元素是正无穷的矩阵。
单位元就是和其他任何元素操作一次,结果都是那个元素本身的元素。*/

int main() {
    IO;
    cin >> k >> m >> S >> E;
    memset(g, 0x3f, sizeof g);
    map<int, int> ids;
    if (!ids.count(S)) ids[S] = ++n;
    if (!ids.count(E)) ids[E] = ++n;
    S = ids[S], E = ids[E];
    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] << '\n';
    return 0;
}
posted @ 2021-02-14 15:19  phr2000  阅读(47)  评论(0编辑  收藏  举报