Floyd-Warshall

就是求所有顶点对的最短路径长度



#include <iostream>
#include <vector>

using namespace std;

typedef struct{
int vexs[10];
int edges[10][10];
int n;
int e;
}MGraph;

#define INFINITE 2048

void CreateGraphM(MGraph *G){

int N1,N2;

cout<<"Enter the number of vertexs and edges: "<<endl;
cin>>(G->n)>>(G->e);

for(int i=0;i<G->n;i++)
cin>>(G->vexs[i]);

for(int i=0;i<G->n;i++)
for(int j=0;j<G->n;j++)
G->edges[i][j]=-1;

cout<<"EDGES: "<<endl;

for(int k=0;k<G->e;k++){
int weight;
cin>>N1>>N2>>weight;
G->edges[N1-1][N2-1]=weight;
}

return;
}

vector< vector<int> > Floyed_Warshall(MGraph *G){

vector< vector<int> > Dist(G->n,vector<int>(G->n));

for(int i=0;i<G->n;i++)
for(int j=0;j<G->n;j++)
Dist[i][j] = G->edges[i][j];

for(int i=0;i<G->n;i++)
Dist[i][i] = 0;

for(int i=0;i<G->n;i++)
for(int j=0;i<G->n;i++)
for(int k=0;i<G->n;i++)
if(Dist[j][i] + Dist[i][k] < Dist[j][k] && \
Dist[j][i] != -1 && \
Dist[i][k] != -1)
Dist[j][k] = Dist[j][i] + Dist[i][k];

return Dist;
}

int main()
{
vector< vector<int> > Dist;
MGraph *G = new MGraph;

CreateGraphM(G);

Dist = Floyed_Warshall(G);

cout<<endl;

for(int i=0;i<G->n;i++)
for(int j=0;j<G->n;j++)
if(Dist[i][j]!=-1 && i!=j)
cout<<i+1<<" "<<j+1<<" "<<Dist[i][j]<<endl;

cout<<endl;

delete G;

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值