HDU - 1599 - find the mincost route (floyd find the smallest ring)

Title link
  within the first heavy cycle plus Common floyd section of code, for the three points formed by the smallest ring, there is a problem that the pit will increase due to the three variables, INF carefully too loud explosion 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;
}

Guess you like

Origin www.cnblogs.com/shuitiangong/p/12584838.html