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. 关于spring mybateis 定义resultType="java.util.HashMap"

    关于spring mybateis  定义resultType="java.util.HashMap" List<HashMap<String, Object>& ...

  2. Visual simultaneous localization and mapping: a survey 论文解析(全)

    当激光或声纳等距离传感器被用来构建小的静态环境的二维地图时,SLAM的问题被认为是解决的.然而,对于动态,复杂和大规模的环境,使用视觉作为唯一的外部传感器,SLAM是一个活跃的研究领域. 第一部分是简 ...

  3. Python连接webstocker获取消息

    简介(脚本都是根据网上资料改写) 此脚本主要是客户觉得webstcket不稳定,所以编辑一个脚本,不停的请求web服务器,当发生错误时,脚本自动退出(). 脚本内容 脚本一 # -*- coding: ...

  4. C# Split用法

    1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...

  5. 初识JavaScript(一)

    初识JavaScript(一) 最近由于工作的需要的原因,我从一个写后台的现在让我转到写前端,再加上我的js部分特别的差,所以我现在开始学习js部分的知识. 我的第一篇博文就这样开始写了.俗话说,千里 ...

  6. Python爬虫通过替换http request header来欺骗浏览器实现登录

    以豆瓣为例,访问https://www.douban.com/contacts/list 来查看自己关注的人,要登录才能查看. 如果用requests.get()方法获取这个http,没登录只能抓取回 ...

  7. Javascript高级程序设计笔记 <第五章> 引用类型

    一.object类型 创建object实例的方式有两种: //第一种使用new操作符跟构造函数 var person= new Object(); person.name="小王" ...

  8. ES6(四)字符串的扩展

    1.字符的表示方式 最早在  \u0000-\uFFFF 之间的字符已经足够使用吗,每个字符占两个字节,超出范围,必须使用双字节形式表达, 即每个字符占四个字节.超出范围的字符,会被解读成  \uXX ...

  9. SecureCRT 历史版本下载

    最近在使用SecureCRT时,存在网络卡顿现象,然而.同事的SecureCRT工具却一点都不卡,我的SecureCRT是比较老的版本6,同事使用的是版本7,所以就更换下自己的SecureCRT版本. ...

  10. JavaScript Dom入门

    好像代码太杂了,博客园里跑不起来,单独复制到html中本地测试都是没有问题的. JavaScript JavaScript 是属于 web 的语言,它适用于 PC.笔记本电脑.平板电脑和移动电话. J ...