# 图Floyd 多源最短路径求解 C++实现

/*
* File name  : graph_floyd.cpp
* Function   : 图的学习, Floyd 多源最短路径求解   C++实现
* Created on : 2016年6月01日
* Author     : beijiwei@qq.com

*/
#include <cstdio>
#include <iostream>
#pragma warning(disable:4996)

using namespace std;

#define VMAX 10
#define INFY 65535

int map[VMAX][VMAX];

int main(int argc, char** argv)
{
int v_num, e_num;
int i = 0, j = 0,k=0,weight=0;
freopen("input.txt", "r", stdin);

cin >> v_num >> e_num;

for (int i = 1; i <= v_num; i++)//初始化邻接矩阵
{
for (int j = 1; j <= v_num; j++)
{
if (i == j)
map[i][j] = 0;
else
map[i][j] = INFY;
}
}

for (int k = 1; k <= e_num; k++)//读入边的权值
{
cin >> i >> j >> weight;
map[i][j] = weight;
}
/********************************************************************/
//Floyd
for (k = 1; k <= v_num; k++)
{
for (i = 1; i <= v_num; i++)
{
for (j = 1; j <= v_num; j++)
{
if (map[i][j] > map[i][k] + map[k][j])
{
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
/********************************************************************/
for (int i = 1; i <= v_num; i++)//初始化邻接矩阵
{
for (int j = 1; j <= v_num; j++)
{
cout << "顶点 " << i << " 到顶点 " << j << " 的最短距离是: " << map[i][j] << endl;
}
}

return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：图Floyd 多源最短路径求解 C++实现 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)