acm floyd 最短路径
#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std;
#define INF 0xfffff //因为为了辨别是否有负权,所以INF不能开太大
#define MAX 1100
int path[MAX][MAX];
bool visit[MAX][MAX];
int result;
int floyd (int n) //边较为少而稀疏,能一次性求出所有的顶点到顶点的最短路径
{
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
if(i!=j && j!=k && k!=i)
{
if (path[i][k] + path[k][j] < path[i][j])
{
return 0;
}
else if(path[i][k] + path[k][j] == path[i][j] && !visit[i][j])
{
result--;
visit[i][j] = true;
}
}
}
}
void input(int line)
{
int a, b, weight;
for(int i=1; i<=line; i++)
{
for(int j=1; j<=line; j++)
{
scanf("%d", &weight);
path[i][j] = weight;
visit[i][j] = false;
}
}
}
int main()
{
//freopen("read.txt", "r", stdin);
int T;
scanf("%d", &T);
int co = 1;
while(T--)
{
int n;
scanf("%d", &n);
result = n*n -n;
input(n);
int tep = floyd(n);
printf("Case %d: ", co++);
if(tep == 0)
printf("impossible\n");
else
{
printf("%d\n", result);
}
}
return 0;
}
附件列表