HDU 4034 Graph

来源:互联网 发布:微信助力软件 编辑:程序博客网 时间:2024/05/24 04:54

Description

Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexes, can you find the original graph?

Input

The first line is the test case number T (T ≤ 100). 
First line of each case is an integer N (1 ≤ N ≤ 100), the number of vertexes. 
Following N lines each contains N integers. All these integers are less than 1000000. 
The jth integer of ith line is the shortest path from vertex i to j. 
The ith element of ith line is always 0. Other elements are all positive. 

Output

For each case, you should output “Case k: ” first, where k indicates the case number and counts from one. Then one integer, the minimum possible edge number in original graph. Output “impossible” if such graph doesn't exist. 

Sample Input

330 1 11 0 11 1 030 1 3 4 0 27 3 030 1 41 0 24 2 0

Sample Output

Case 1: 6Case 2: 4Case 3: impossible

本题是找所需要的路径数,关键在于消去不必要的边。就依次找,看某点与其他点的连线是否可以通过其他点间的连线来找到,如果能的话就把总的边数减一。所以就应该找最短距离,如果最短距离等于其就可以减去1,如果小于就不成立,如果大于就不减。(关键在于理解题目中所给的条件都已经是最短边,我们就相当于验证他是不是最短边,如果通过其他边也可以达到最短边的长度那这最短边就没必要存在就减去,如果甚至可以达到比他小,那就说明条件出错,直接输出impossible就行,如果大于的话,那就说明这条边是存在的,放那就好)

#include <stdio.h>#include <string.h>int ans=0;int ma[110][110];int f[110][110];int n;int floyed(){int i,j,k;for(i=0;i<n;i++)for(j=0;j<n;j++){if(i==j)continue;for(k=0;k<n;k++){if(k==j||k==i)continue;if(f[i][j]==f[i][k]+f[k][j]){ans--;break;}else if(f[i][k]+f[k][j]<f[i][j])return 0;}}return 1;}int main(){int t,icase;scanf("%d",&t);for(icase=1;icase<=t;icase++){memset(ma,0,sizeof(ma));int i,j,k;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++){scanf("%d",&ma[i][j]);f[i][j]=ma[i][j];}ans=n*(n-1);int flag=floyed();if(flag)printf("Case %d: %d\n",icase,ans);elseprintf("Case %d: impossible\n",icase);}return 0;}


0 0