hdu 4034 2011成都赛区网络赛 逆向floyd **

时间:2022-05-21 17:00:26

给出一个最短路邻接矩阵,求出构图的最小边数

正常的floyd的k放在最外面是为了防止i到j的距离被提前确定,而逆向的floyd,i到j的距离已经确定,所以需要在i到j之间枚举k,注意需要break,否则会多删除

Sample Input

3
3
0 1 1
1 0 1
1 1 0
3
0 1 3
4 0 2
7 3 0
3
0 1 4
1 0 2
4 2 0 Sample Output
Case 1: 6
Case 2: 4
Case 3: impossible
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt,ans;
int c[MAXN][MAXN];
bool fun()
{
ans=;
int i,j,k;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
for(k=;k<n;k++)
{
if(i!=j&&i!=k&&j!=k)
{
if(c[i][k]+c[k][j]==c[i][j])
{
ans--;
break;
}
if(c[i][k]+c[k][j]<c[i][j]) return ;
} }
}
}
return ;
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
int ca=;
while(tt--)
{
scanf("%d",&n);
printf("Case %d: ",ca++);
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
scanf("%d",&c[i][j]);
}
}
if(fun())
{
printf("%d\n",n*n+ans-n);
}
else printf("impossible\n");
}
}