hdu 4034 Graph (floyd的深入理解)

Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1927    Accepted Submission(s): 965

Problem 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

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

Source

The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest

题意:  给你一些顶点的最短距离,要你求出原图最少含有多少边。

我们知道,最短路所构成的图已经是最少的边。所以只需要用n个点构成的有向边的和n*(n-1)减去那些合成的边。就是最少的边。

采用floy算法,松弛度来勾结一个最短图...

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define maxn 110
 4 int ds[maxn][maxn];
 5 bool vis[maxn][maxn];
 6 int mat[maxn][maxn];
 7 void floyd(int n)
 8 {
 9     for(int k=0;k<n;k++)
10     {
11         for(int i=0;i<n;i++)
12         {
13             if(i==k) continue;
14             for(int j=0;j<n;j++)
15             {
16                 if(k==j)continue;
17                 if(ds[i][j]>=ds[i][k]+ds[k][j])
18                 {
19                     vis[i][j]=1;
20                     ds[i][j]=ds[i][k]+ds[k][j];
21                 }
22             }
23         }
24     }
25 }
26 int main()
27 {
28  int cas,n;
29    scanf("%d",&cas);
30    for(int tt=1;tt<=cas;tt++)
31    {
32          scanf("%d",&n);
33          for(int i=0;i<n;i++)
34           for(int j=0;j<n;j++)
35         {
36           scanf("%d",mat[i]+j);
37           ds[i][j]=mat[i][j];
38         }
39         memset(vis,0,sizeof(vis));
40         floyd(n);
41         int res=0;
42         bool tag=0;
43       for(int i=0;i<n;i++)
44       {
45           for(int j=0;j<n;j++)
46         {
47           if(vis[i][j]&&ds[i][j]==mat[i][j])
48               res++;
49           else if(ds[i][j]<mat[i][j])
50           {
51                 tag=1;
52               break;
53           }
54         }
55          if(tag)break;
56       }
57         printf("Case %d: ",tt);
58         if(tag)
59             printf("impossible\n");
60         else printf("%d\n",n*(n-1)-res);
61 
62    }
63  return 0;
64 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏算法修养

UVALive 6933 Virus synthesis(回文树)

Viruses are usually bad for your health. How about ghting them with... other vir...

3627
来自专栏ml

hdu---1024Max Sum Plus Plus(动态规划)

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

32710
来自专栏小樱的经验随笔

POJ 2209 The King(简单贪心)

The King Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7499...

2844
来自专栏ml

hdu----(3068)最长回文(manacher)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

2975
来自专栏ml

hdu 4033Regular Polygon(二分+余弦定理)

Regular Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65...

3376
来自专栏算法修养

HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 655...

51116
来自专栏算法修养

HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submission...

2944
来自专栏HansBug's Lab

3101: N皇后

3101: N皇后 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 88 ...

3409
来自专栏算法修养

HDU 2476 String painter(区间DP)

String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/327...

3468
来自专栏算法修养

POJ-1125 Stockbroker Grapevine

Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Sub...

3124

扫码关注云+社区

领取腾讯云代金券