Graph (floyd)
Description
Input
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
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求好最短路的图,问最少可由多少条边得到。
题目解析:对于边e[i][j],如果存在e[i][j]=e[i][k]+e[k][j],则边i->j没有必要存在;如果存在e[i][j]>e[i][k]+e[k][j],则图有误,impossible。将最外层循环放到最内层即可。 代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
int mp[][],n;
int ok()
{
int i,j,k;
int cnt=,vnt=;
for(i=;i<=n;++i){ ///枚举每
for(j=;j<=n;++j){ ///一条边
if(mp[i][j]!=)
++vnt;
if(i==j)
continue;
for(k=;k<=n;++k){ ///枚举中间节点
if(i==k||j==k)
continue;
if(mp[i][j]>mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
return -;
}else if(mp[i][j]==mp[i][k]+mp[k][j]&&mp[i][k]&&mp[k][j]){
++cnt;
break;
}
}
}
}
return vnt-cnt;
}
int main()
{
int T,i,j,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;++i)
for(j=;j<=n;++j)
scanf("%d",&mp[i][j]);
printf("Case %d: ",++cas);
int ans=ok();
if(ans!=-)
printf("%d\n",ans);
else
printf("impossible\n");
}
return ;
}
Graph (floyd)的更多相关文章
- [CodeForces - 296D]Greg and Graph(floyd)
Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点 ...
- Graph(Floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others) Memor ...
- WUSTOJ 1326: Graph(Java)费马数
题目链接:1326: Graph 参考博客:HNUSTOJ-1617 Graph(费马数)--G2MI Description Your task is to judge whether a regu ...
- (floyd)佛洛伊德算法
Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...
- POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...
- 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 ...
- Stockbroker Grapevine(floyd)
http://poj.org/problem?id=1125 题意: 首先,题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时, 输入数据结束),然后接下来N行描述第i(1< ...
- 2018 ICPC 沈阳网络预赛 Fantastic Graph (优先队列)
[传送门]https://nanti.jisuanke.com/t/31447 [题目大意]:有一个二分图,问能不能找到它的一个子图,使得这个子图中所有点的度数在区间[L,R]之内. [题解]首先我们 ...
- Floyed(floyd)算法详解
是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...
随机推荐
- MySQL数据库----数据锁
数据锁 需求: 有一个账户,两个人在同一时间要对此账户操作,A要对账户充值100块,B要从账户中取出100块.操作前都要先看一下账户的 余额然后再操作. -- 窗口1 用户进行充值 -- 充值前 先查 ...
- MySQL Crash Course #14# Chapter 22. Using Views
索引 视图是啥 为什么需要视图 使用视图的规则 如何使用视图 视图应用实例 别用视图更新数据! 视图是啥 理解视图的最佳方式就是看下面这个例子. SELECT cust_name, cust_cont ...
- 20145329 《网络对抗技术》Web安全基础实践
实践的目标 理解常用网络攻击技术的基本原理.Webgoat实践下相关实验:SQL注入攻击.XSS攻击.CSRF攻击. 实验后回答问题 (1)SQL注入攻击原理,如何防御 攻击原理 SQL注入即是指we ...
- ["1", "2", "3"].map(parseInt) 为何返回[1,NaN,NaN]
转载自:http://blog.csdn.net/freshlover/article/details/19034079 这涉及到是否深入理解两个函数的格式与参数含义. 首先根据我对两个函数用法的了解 ...
- 51nod 1043 幸运号码(数位dp
1043 幸运号码 1个长度为2N的数,如果左边N个数的和 = 右边N个数的和,那么就是一个幸运号码. 例如:99.1230.123312是幸运号码. 给出一个N,求长度为2N的幸运号码的数量 ...
- SPA (单页应用程序)
单页Web应用 编辑 单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.单页应用程序 (SPA) 是加载单个HTML 页面并在用户与应用程 ...
- Linux 安装 mysql 转 http://www.cnblogs.com/fnlingnzb-learner/p/5830622.html
到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64位系统下载Linux - Generic (g ...
- Java之Elasticsearch 增删改查
<!--ELK --> <dependency> <groupId>org.elasticsearch.client</groupId> <art ...
- 2018/7/18Bad English
1 So instead of enjoying anime because it can portray interesting stories in a form that would be im ...
- BZOJ 1015: [JSOI2008]星球大战starwar(并查集求连通块+离线处理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1015 题意: 思路:好题啊!!! 这道题目需要离线处理,先把所有要删的点给保存下来,然后逆序加点,这 ...