zoukankan      html  css  js  c++  java
  • Floyd 算法 打印路径模板

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <cstring>
    using namespace std;
    #define INF 0xfffffff
    #define maxn 40
    
    int G[maxn][maxn], Path[maxn][maxn], n;
    
    void Floyd()
    {
        for(int k=1; k<=n; k++)
        {
            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];
                        Path[i][j] = Path[i][k];
                    }
                }
            }
        }
    }
    void PutPath(int Star,int End)
    {
        while(Star != End)
        {
            printf("%d---->", Star);
            Star = Path[Star][End];
        }
        printf("%d
    ", End);
    }
    void Init()
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                Path[i][j] = j;
            }
        }
    }
    
    int main()
    {
    
        cin >> n;
    
        Init();
    
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cin >> G[i][j];
                if(G[i][j] == -1)
                    G[i][j] = INF;
            }
        }
        Floyd();
    
    
        PutPath(1,n);
    
        printf("%d
    ", G[1][n]);
        return 0;
    }
    /*
    4
    -1 1 -1 -1
    -1 -1 1 -1
    -1 -1 -1 1
    -1 -1 -1 -1
    */
  • 相关阅读:
    P1726 上白泽慧音
    P1993 小k的农场
    P1983 车站分级
    P1525 关押罪犯【二分+二分图】
    P1268 树的重量【构造】
    P1113 杂务
    F.Three pahs on a tree
    P1522 牛的旅行
    两个约束下的dp问题
    dp 最大正方形
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4173352.html
Copyright © 2011-2022 走看看