Graph(Floyd)

http://acm.hdu.edu.cn/showproblem.php?pid=4034

Graph

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

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

题意:给出由已知点求出的每个点间的最短路,问你原先的图中最少有几个点

题解:对已经给出的最短路再求一遍最短路用Floyd ,如果在求得过程中发现有dist[i][j]>dist[i][k]+dist[k][j]的情况就说明所给的不是最短的路图,及impossible

而在求解的过程中,当dist[i][j]==dist[i][k]+dist[k][j]的时候说明从i 到j 的长度,可以通过k点到达,故可以将直接相连的i,j去掉,及标记dist[i][j] = INF;

注意两点: 1,可以先将impossible的情况单独先算出来,以防后面对dist[i][j]  = INF ;

2, 当i==j||j==k||j==k 的时候要continue掉,因为这个为0的点会更新其他所有的点

下面是代码

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 103
 6 #define INF 0x1fffffff
 7 int mp[N][N];
 8 int dist[N][N];
 9 int main()
10 {
11     int i , j , k ;
12     int n;
13     int t ;
14     cin>>t;
15     int c = 0;
16     while(t--)
17     {
18         c++;
19         scanf("%d",&n);
20         for( i = 0 ; i < n ;i++)
21         {
22             for(  j = 0 ; j < n ;j++)
23             {
24                 scanf("%d",&mp[i][j]);
25                 dist[i][j] = mp[i][j];
26             }
27         }
28         bool flag = true;
29         for(k = 0 ;flag && k < n ; k++)
30         {
31             for(i = 0 ;flag && i < n ; i++)
32             {
33                 for( j = 0 ; flag&& j < n ;j++)
34                 {
35                     if(dist[i][j]>dist[i][k]+dist[k][j])
36                         flag = false;
37                 }
38             }
39         }
40         int cnt = 0;
41         if(flag)
42         {
43             for( k = 0 ; k < n ;k++)
44             {
45                 for(i = 0 ; i < n ;i++)
46                 {
47                     for(j = 0 ;j < n ;j++)
48                     {
49                         if(i==j||j==k||k==i) continue;
50                         if(dist[i][j]==dist[i][k]+dist[k][j])
51                             {
52                                 dist[i][j] = INF;
53                                 //printf("%d %d %d\n", k ,i , j);
54                                 cnt++;
55                             }
56                     }
57                 }
58             }
59         }
60         if(flag) printf("Case %d: %d\n",c,n*(n-1)-cnt);
61         else printf("Case %d: impossible\n",c);
62
63     }
64     return 0 ;
65 }
时间: 2024-08-08 01:29:14

Graph(Floyd)的相关文章

【CodeForces - 296D】Greg and Graph(floyd)

Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点之前的所有两点间最短距离和.n<=500 Solution 如果暴力打肯定是会超时的,那就要运用到floyd(hj) floyd算法内2个循环就相当于新加入外循环的那个点然后跟新最短路, 所以可以把题目看成倒过来依次加点,每次\(n^2\)平方更新一下,总共\(O(n^3)\) Code #incl

【UVA】821-Page Hopping(Floyd)

模板题,求一个点到任何一点的距离,用Floyd就行了,结点不一定是从1 ~ n 的,所以需要记录结点的id 14063895 821 Page Hopping Accepted C++ 0.119 2014-08-19 10:00:27 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<sta

hdu 1690 Bus System(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Problem Description Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it's

人活着系列之开会 (floyd)

人活着系列之开会 Time Limit: 1000MS Memory limit: 65536K 题目描述 人活着如果是为了事业,从打工的到老板的,个个都在拼搏,奋斗了多年终于有了非凡成就,有了一笔丰富的钱财.反过来说,人若赚取了全世界又有什么益处呢?生不带来,死了你还能带去吗?金钱能买保险,但不能买生命,金钱能买药品,但不能买健康,人生在世,还是虚空呀! 在苍茫的大海上,有很多的小岛,每个人都在自己的小岛上.又到了开会的时候了,鹏哥通过飞信告知了每个人,然后大家就开始往鹏哥所在的主岛走,问谁先

hdu 1869 (Floyd)

http://acm.hdu.edu.cn/showproblem.php?pid=1869 六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4355    Accepted Submission(s): 1768 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象

(floyd)佛洛伊德算法

Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的三重循环,而且纯粹的Floyd算法的循环体内的语句也十分简洁.我认为,正是由于“Floyd算法是一种动态规划(Dynamic Programming)算法”的本质,才导致了Floyd算法如此精妙.因此,这里我将从Floyd算法的状态定义.动态转移方程以及滚动数组等重要方面,来简单剖析一下图论中这一重

最短路径之弗洛伊德算法(Floyd)

Floyd算法又称为插点法,是一种用于寻找给定的加权图中多源点之间最短路径的算法. 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵. 从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归的进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1): 又用同样地公式由D(1)构造出D(2):--:最后又用同样的公式由D(n-1)构造出矩阵D(n).矩阵D(n) 的 i 行 j 列元素便是 i 号顶点到 j 号顶点的最短路径长度,称D(n)为图的距离矩阵,同时还可引入一个后

1035.找出直系亲属(floyd)

题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-. 输入:     输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50)

2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)

[传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们分这几种情况讨论: (1)如果集合U,V中存在某个点,它的度数小于L,那么肯定就不满足题意,直接输出No.所以对任意i, degree[i] >= L (2)如果集合U,V中所有点的度数都在给定区间内,直接输出Yes. (3)如果集合U,V中存在某些点的度数大于R,则需要减少与它关联的边,直到它的度