HDU - 1599 - find the mincost route (floyd求最小环)

题目链接
  在普通的floyd的第一重循环内加上的一段代码,求3点形成的最小环,这题有个坑点是因为会加三个变量, INF小心开太大爆int。

const int INF = INT_MAX/10;
const int maxn = 1e3+10;
int g[maxn][maxn], g2[maxn][maxn], n, m, minr;
void init() {
    for (int i = 1; i<=n; ++i)
        for (int j = 1; j<=n; ++j)
            g2[i][j] = g[i][j] = INF;
    minr = INF;
}
void floyd() {
    for (int k = 1; k<=n; ++k) {
        for (int i = 1; i<k; ++i)
            for (int j = 1; j<i; ++j)
                if (minr > g[i][j] + g2[j][k] + g2[k][i])
                    minr = g[i][j] + g2[j][k] + g2[k][i];
        for (int i = 1; i<=n; ++i)
            for (int j = 1; j<=n; ++j)
                if (g[i][j] > g[i][k] + g[k][j])
                    g[i][j] = g[i][k] + g[k][j];
    }
}
int main(void) {
    while(~scanf("%d%d", &n, &m)) {
        init();
        for (int i = 0, u, v, c; i<m; ++i) {
            scanf("%d%d%d", &u, &v, &c);
            g2[u][v] = g2[v][u] = g[u][v] = g[v][u] = min(g[u][v], c);
        }
        floyd();
        if (minr==INF) printf("It's impossible.\n");
        else printf("%d\n", minr);
    }
    return 0;
}
posted @ 2020-03-27 22:36  shuitiangong  阅读(115)  评论(0编辑  收藏  举报