码迷,mamicode.com
首页 > 其他好文 > 详细

floyd求路径

时间:2018-07-21 22:38:02      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:路径   col   turn   pre   const   top   print   path   scan   

#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;

const int maxn=1000;
const int nil=20000000;

int a[maxn][maxn],path[maxn][maxn];
stack<int>s;


int main(){
    int n;
    int m,x,y,w;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++) a[i][j]=nil,path[i][j]=-1;
    for (int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&w);
        a[x][y]=w;
        a[y][x]=w;
    }
    for (int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                if(a[i][j]>(a[i][k]+a[k][j])&&i!=j&&i!=k&&k!=j) {
                    a[i][j]=a[i][k]+a[k][j];
                    path[i][j]=k;
                }
            }
        }
    }
    printf("%d\n",a[1][4]);
    printf("%d ",1);
    int k=4;
    while(path[1][k]!=-1){
        k=path[1][k];
        s.push(k);
    }
    while(!s.empty()){
        printf("%d ",s.top());
        s.pop();
    }
    printf("%d ",4);
return 0;
}

 

floyd求路径

标签:路径   col   turn   pre   const   top   print   path   scan   

原文地址:https://www.cnblogs.com/lmjer/p/9348142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!