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 = ;
  16. while(t--)
  17. {
  18. c++;
  19. scanf("%d",&n);
  20. for( i = ; i < n ;i++)
  21. {
  22. for( j = ; 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 = ;flag && k < n ; k++)
  30. {
  31. for(i = ;flag && i < n ; i++)
  32. {
  33. for( j = ; 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 = ;
  41. if(flag)
  42. {
  43. for( k = ; k < n ;k++)
  44. {
  45. for(i = ; i < n ;i++)
  46. {
  47. for(j = ;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-)-cnt);
  61. else printf("Case %d: impossible\n",c);
  62.  
  63. }
  64. return ;
  65. }

Graph(Floyd)的更多相关文章

  1. [CodeForces - 296D]Greg and Graph(floyd)

    Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...

  2. Graph (floyd)

    Description Everyone knows how to calculate the shortest path in a directed graph. In fact, the oppo ...

  3. WUSTOJ 1326: Graph(Java)费马数

    题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...

  4. (floyd)佛洛伊德算法

    Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...

  5. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  6. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  7. Stockbroker Grapevine(floyd)

    http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...

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

    [传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们 ...

  9. Floyed(floyd)算法详解

    是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...

随机推荐

  1. 为了CET-4!

    Directions For tiis part,you are allowed 30 minutes to write an essay.Suppose there are two options ...

  2. 深度优先搜索(DFS)——部分和问题

    对于深度优先搜索,这里有篇写的不错的博客:DFS算法介绍 .总得来说是从某个状态开始,不断的转移状态知道无法转移,然后回到前一步的状态.如此不断的重复一直到找到最终的解.根据这个特点,常常会用到递归. ...

  3. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  4. ES6 函数的扩展1

    1. 函数参数的默认值 基本用法 在ES6之前,不能直接为函数的参数指定默认值,为了避免这个问题,通常需要先判断一下参数y是否被赋值,如果没有,再等于默认值. ES6允许为函数的参数设置默认值,即直接 ...

  5. 2.Nginx日常维护技巧

    Nginx日常维护技巧 Nginx配置正确性检查 nginx提供了配置文件调试功能,可以快速定义配置文件存在的问题.执行如下命令检测配置文件的正确性: [root@localhost 桌面]# whi ...

  6. 高仿二次元网易GACHA

    高仿二次元网易GACHA,所有接口均通过Charles抓取而来,图片资源通过 https://github.com/yuedong56/Assets.carTool 工具提取. 详情见github地址 ...

  7. pyshark 得到payload

    mydata = pkt[okt.highest_layer].data mydata.decode("hex")

  8. Spring3.x企业开发应用实战读书笔记 —— 第三章IoC容器概述

    声明:    本篇博客绝大多数内容为<Spring3.x企业开发应用实战>一书原内容,所有版权归原书作者所有!,仅供学习参考,勿作他用! 3.2 相关Java基础知识 Java语言允许通过 ...

  9. Head First设计模式之生成器模式

    一.定义 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一种对象创建型模式. 二.结构 角色 Builder(抽象建造者):它为创建一个产品Product对象的 ...

  10. Linux 多线程下载工具:axel

    wget 应该是最常用的下载工具了,但是其不支持多线程下载. axel 安装 epel 源有 axel 的二进制包,可以使用 yum 安装. yum install epel-release yum ...