这个题主要就是裸的floyd大法 注意就是不要计算了重边 所以必须用vis标记...orz
#include<cstdio>
int line[101][101];
int ver;
int floyd()
{
int cnt=0;
bool vis[101][101]={0};
for(int k=1;k<=ver;k++){
for(int i=0;i<=ver;i++){
for(int j=0;j<=ver;j++){
if(i==j||j==k||k==i)continue;
if(!vis[i][j]&&line[i][j]==line[i][k]+line[k][j]){
vis[i][j]=1;
cnt++;
}
if(line[i][j]>line[i][k]+line[k][j])return -1;
}
}
}
return cnt;
}
int main()
{
int cas,i,j,k,ans;
scanf("%d",&cas);
for(i=0;i<cas;i++){
scanf("%d",&ver);
for(j=1;j<=ver;j++){
for(k=1;k<=ver;k++)scanf(" %d",&line[j][k]);
}
ans=floyd();
if(ans==-1)printf("Case %d: impossible\n",i+1);
else {
printf("Case %d: %d\n",i+1,ver*(ver-1)-ans);
}
}
return 0;
}