H - Graph
Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u
Appoint description:
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.
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
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
给你一个最短路的图 问原图最小的边的个数///刷dp专题抓错题也是尴尬- -
ACcode:
#include <cstdio>
#include <iostream>
#include <algorithm>
#define maxn 104
using namespace std;
int dp[maxn][maxn];
int main(){
int loop,cnt=1;
scanf("%d",&loop);
while(loop--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
scanf("%d",&dp[i][j]);
bool flag=false;
int ans=0;
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(i!=j)
if(dp[i][k]+dp[k][j]<dp[i][j])
flag=true;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i==j) continue;
bool mark=true;
for(int k=1;mark&&k<=n;k++)
if(j!=k&&i!=k&&dp[i][j]==dp[i][k]+dp[k][j])
mark=false;
if(mark) ans++;
}
printf("Case %d: ",cnt++);
if(flag)puts("impossible");
else printf("%d\n",ans);
}
return 0;
}