floyd算法求最短路径matlab程序,Floyd算法求最短路径

/*--------------------denghui-2015.11.16---------------------------*/

/*input: a int number n, means the number of node on the graph

n(row)*n(col) (int) , means the graph matrix

s(int), means the number of test data

s(row)*2(col) (int), means start and end

like this:

4

0 2 10 10000

2 0 7 3

10 7 0 6

1000 3 6 0

2

0 2

3 0

*/

#include

using namespace std;

#define MAX 30

int readdata(int Graph[][MAX]);

void search(int Graph[][MAX], int Path[][MAX], int n);

void display(int Path[][MAX], int n);

void putout(int Path[][MAX], int start, int end);

int main()

{

int Graph[MAX][MAX];

int n = readdata(Graph);

int Path[MAX][MAX];

search(Graph, Path, n);

display(Path, n);

return 0;

}

int readdata(int Graph[][MAX])

{

int n;

cin >> n;

int i, j;

for (i = 0; i < n; ++i) {

for (j = 0; j < n; ++j) {

cin >> Graph[i][j];

}

}

return n;

}

void search(int Graph[][MAX], int Path[][MAX], int n)

{

int point[MAX][MAX];

int i, j, k;

for (i = 0; i < n; ++i) { // init

for (j = 0; j < n; ++j) {

point[i][j] = Graph[i][j];

Path[i][j] = -1;

}

}

for (i = 0; i < n; ++i) {

for (j = 0; j < n; ++j) {

for (k = 0; k < n; ++k) {

if (point[j][i] + point[i][k] < point[j][k]) {

point[j][k] = point[j][i] + point[i][k];

Path[j][k] = i;

}

}

}

}

}

void display(int Path[][MAX], int n)

{

int start, end, s;

cin >> s;

while (s > 0) {

s--;

cin >> start >> end;

putout(Path, start, end);

cout << end << endl;

}

}

void putout(int Path[][MAX], int start, int end)

{

if (Path[start][end] == -1) {

cout << start << endl;

} else {

putout(Path, start, Path[start][end]);

putout(Path, Path[start][end], end);

}

}

//end

/*=============================Denghui=================================*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值