洛谷P6175 无向图的最小环问题 题解 FLoyd变形题

题目链接:https://www.luogu.com.cn/problem/P6175

题解完全参照自 kami2004 的博客 大神的博客:https://www.luogu.com.cn/blog/kami2004/solution-p6175

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, INF = (1<<29);
int n, m, g[maxn][maxn], dis[maxn][maxn], ans = INF;
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= n; j ++) {
            g[i][j] = dis[i][j] = (i == j) ? 0 : INF;
        }
    }
    while (m --) {
        int u, v, w;
        cin >> u >> v >> w;
        dis[u][v] = dis[v][u] = g[u][v] = g[v][u] = min(g[u][v], w);
    }
    for (int k = 1; k <= n; k ++) {
        for (int i = 1; i <= n; i ++) {
            for (int j = 1; j <= n; j ++) {
                if (i != j && j != k && i != k && dis[i][j] < INF && g[i][k] < INF && g[k][j] < INF)
                    ans = min(ans, dis[i][j] + g[i][k] + g[k][j]);
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }
    if (ans == INF) puts("No solution.");
    else cout << ans << endl;
    return 0;
}
posted @ 2020-05-22 00:58  quanjun  阅读(172)  评论(0编辑  收藏  举报