uva 125

floyd 算法   如果存在无数条路  则存在a->a的路  a->b的路径数等于 a->i 和 i->b(0=<i<=_max) 路径数的乘积和

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 30+5;
int n, g[maxn][maxn],ca;
int main()
{
ca = 0;
while (scanf("%d", &n) == 1)
{
int _max = 0;
memset(g, 0, sizeof(g));
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = 1;
_max = max(_max, max(x, y));
}
for (int k = 0; k <= _max; k++)
for (int i = 0; i <= _max; i++)
for (int j = 0; j <= _max; j++)
g[i][j] += g[i][k]*g[k][j];
for (int k = 0; k <= _max; k++)
if (g[k][k])
for (int i = 0; i <= _max; i++)
for (int j = 0; j <= _max; j++)
if (g[i][k] && g[k][j])
g[i][j] = -1;
printf("matrix for city %d\n", ca++);
for (int i = 0; i <= _max; i++)
{
for (int j = 0; j <= _max; j++)
{
if (j) putchar(' ');
printf("%d", g[i][j]);
}
puts("");
}
}
return 0;
}

上一篇:STM32学习笔记2-系统时钟知识及程序配置


下一篇:Examples_08_07