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的点会更新其他所有的点

下面是代码

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 103
#define INF 0x1fffffff
int mp[N][N];
int dist[N][N];
int main()
{
int i , j , k ;
int n;
int t ;
cin>>t;
int c = ;
while(t--)
{
c++;
scanf("%d",&n);
for( i = ; i < n ;i++)
{
for( j = ; j < n ;j++)
{
scanf("%d",&mp[i][j]);
dist[i][j] = mp[i][j];
}
}
bool flag = true;
for(k = ;flag && k < n ; k++)
{
for(i = ;flag && i < n ; i++)
{
for( j = ; flag&& j < n ;j++)
{
if(dist[i][j]>dist[i][k]+dist[k][j])
flag = false;
}
}
}
int cnt = ;
if(flag)
{
for( k = ; k < n ;k++)
{
for(i = ; i < n ;i++)
{
for(j = ;j < n ;j++)
{
if(i==j||j==k||k==i) continue;
if(dist[i][j]==dist[i][k]+dist[k][j])
{
dist[i][j] = INF;
//printf("%d %d %d\n", k ,i , j);
cnt++;
}
}
}
}
}
if(flag) printf("Case %d: %d\n",c,n*(n-)-cnt);
else printf("Case %d: impossible\n",c); }
return ;
}

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. HTML列表的常用属性及其应用

    首先列表分成有序和无序分别是<ol><ul>,无序的比较简单,看个例子: <html> <body> <h4>一个无序列表:</h4& ...

  2. spring boot + Thymeleaf开发web项目

    "Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...

  3. InnoDB 逻辑存储结构

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/80 如果创建表时没有显示的定义主键,mysql会按如下方式创建主 ...

  4. mysql5.7-Group Replication

    什么是Group Replication 基于组的复制(Group-based Replication)是一种被使用在容错系统中的技术.Replication-group(复制组)是由能够相互通信的多 ...

  5. 某次送温暖考试的 c题

    题目大意: 给定n个点的无根树,树上每个点都有一个非负的点权. 树上的路径的价值定义为树上路径的点权和-树上路径的点权最大值; 现在给定一个参数P询问有多少条路径的价值是P的倍数(注意单点也算路径,路 ...

  6. 第六节 etc/passwd 、etc/shadow 、 useradd 、 groupadd

    调优方法原理:禁用atime的修改来节省cpu和内存资源.命令:mount noatime disk 1.配置文件1. /etc/passwd文档结构:由":" 分隔成7个字段&q ...

  7. CPP--关于long的争议和思考

    先普及一下VS开发Linux的知识点 VS2017的安装:https://www.cnblogs.com/dunitian/p/8051985.html 创建项目在这 第一次运行的时候会让输入服务器信 ...

  8. IT小白学习Discuz!框架(一)

    1.Discuz!是什么? 答:(1).Crossday Discuz! Board(简称 Discuz!)是北京康盛新创科技有限责任公司推出的一套通用的社区论坛软件系统. (2).Crossday ...

  9. Nginx完整配置配置样例【官方版】

    我们主要参考nginx官方给出的完整配置的样例: https://www.nginx.com/resources/wiki/start/topics/examples/full/# 完整摘录如下: n ...

  10. BeanShell断言(一)

    在beanShell中直接可以调用的变量,无需加前缀. 1.log 打印日志 log.info(“在控制台打印日志”); 2.SampleResult 获取SampleResult对象,可以通过这个对 ...