解题思路:
注意事项:
参考代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 55,inf=1e9+10;
int g[N][N], n;
void floyd()
{
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j] == 0&&i!=j)
g[i][j] =inf;
}
}
floyd();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
g[i][j] = 0;
if (g[i][j] > inf / 2)
printf("-1 ");
else
printf("%d ", g[i][j]);
}
puts("");
}
puts("");
return 0;
}
0.0分
1 人评分
1012题解浏览:938 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:567 |
C二级辅导-统计字符 (C语言代码)浏览:695 |
杨辉三角 (C语言代码)浏览:734 |
简单的a+b (C语言代码)浏览:473 |
C二级辅导-公约公倍 (C语言代码)浏览:1325 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:456 |
简单的a+b (C语言代码)浏览:491 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:3254 |
简单的a+b (C语言代码)浏览:363 |