前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu 4034 Graph (floyd的深入理解)

hdu 4034 Graph (floyd的深入理解)

作者头像
Gxjun
发布2018-03-26 15:59:34
6510
发布2018-03-26 15:59:34
举报
文章被收录于专栏:mlml

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算法,松弛度来勾结一个最短图...

代码:

代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-10-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Graph
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档