裸floyd大法

来源:互联网 发布:检查ip端口是否打开 编辑:程序博客网 时间:2024/05/23 23:29

这个题主要就是裸的floyd大法 注意就是不要计算了重边 所以必须用vis标记...orz  


C - D
Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 

#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;}





0 0
原创粉丝点击