Restoring Road Network
D - Restoring Road Network
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are known about the road network:
- People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.
- Different roads may have had different lengths, but all the lengths were positive integers.
Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom. He thought that it represented the shortest distances between the cities along the roads in the kingdom.
Determine whether there exists a road network such that for each u and v, the integer Au,v at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v. If such a network exist, find the shortest possible total length of the roads.
Constraints
- 1≤N≤300
- If i≠j, 1≤Ai,j=Aj,i≤109.
- Ai,i=0
Inputs
Input is given from Standard Input in the following format:
N
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
…
AN,1 AN,2 … AN,N
Outputs
If there exists no network that satisfies the condition, print -1. If it exists, print the shortest possible total length of the roads.
Sample Input 1
3
0 1 3
1 0 2
3 2 0
Sample Output 1
3
The network below satisfies the condition:
- City 1 and City 2 is connected by a road of length 1.
- City 2 and City 3 is connected by a road of length 2.
- City 3 and City 1 is not connected by a road.
Sample Input 2
3
0 1 3
1 0 1
3 1 0
Sample Output 2
-1
As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3. However, according to the table, the shortest distance between City 1 and City 3 must be 3.
Thus, we conclude that there exists no network that satisfies the condition.
Sample Input 3
5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0
Sample Output 3
82
Sample Input 4
3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0
Sample Output 4
Copy
3000000000
//题意:给出一个 n * n 的最短路表,问此表需要最少连通多少边多少才能实现。、
//显然,对于每对点都要考虑,如果,可以通过第三方点实现,就用第三方,否则,只能连本身的边
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define eps 1e-8
#define MX 305 int n;
int G[MX][MX]; int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
scanf("%d",&G[i][j]);
LL ans =;
bool ok=;
for (int i=;i<=n;i++)
{
for (int j=;j<=n;j++)
{
if (i==j) continue;
bool need=;
for (int k=;k<=n;k++)
{
if (k==i||k==j) continue;
if (G[i][j]>G[i][k]+G[k][j]) ok=;
if (G[i][j]==G[i][k]+G[k][j]) need=;
}
if (need) ans+=G[i][j];
}
}
if (ok) printf("%lld\n",ans/);
else printf("-1\n");
}
return ;
}
Restoring Road Network的更多相关文章
- Restoring Road Network Floyd
问题 C: Restoring Road Network 时间限制: 1 Sec 内存限制: 128 MB提交: 731 解决: 149[提交] [状态] [讨论版] [命题人:admin] 题目 ...
- Restoring Road Network(Floyd算法的推广)
个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况, 后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧. 题意概述: 就 ...
- AtCoder Regular Contest 083 D: Restoring Road Network
题意 有一张无向带权连通图(点数<=300),给出任意两点i,j之间的最短路长度dis[i][j].问是否存在一张这样的无向图.如果不存在输出-1.如果存在输出所有这样的无向图中边权和最小的一张 ...
- 【Atcoder】ARC083 D - Restoring Road Network
[算法]图论,最短路? [题意]原图为无向连通图,现给定原图的最短路矩阵,求原图最小边权和,n<=300. [题解]要求最小边权和下,原图的所有边一定是所连两端点的最短路. 那么现在将所有最短路 ...
- 【AtCoder Beginner Contest 074 D】Restoring Road Network
[链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...
- [Arc083D/At3535] Restoring Road Network - 最短路,结论
[Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...
- LiDAR Textbook & Automated Road Network Extraction
Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...
- 据说是Flord算法
贵有恒,何必三更起五更眠:最无益,莫过一日曝十日寒. 问题 C: Restoring Road Network 问题 C: Restoring Road Network 时间限制: 1 Sec 内存 ...
- 【AtCoder】ARC083
C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...
随机推荐
- 火狐 http://localhost:8080自动跳转到http://www.localhost.com:8080
用火狐调试PHP时 偶尔会出现连接被重置的情况:http://localhost:8080自动跳转到http://www.localhost.com:8080 解决方案1: 打开网络-属性,往下拉找到 ...
- LOGO闪光效果
原地址:http://cl314413.blog.163.com/blog/static/1905079762014122105235138/ 这个效果在很多LOGO及广告宣传中都会用到.商业开发的做 ...
- 理解shell中的atime,mtime,ctime
所有文件都有3个时间信息,保存在文件系统中 atime (Access time)是文件最后一此读的时间 或者执行文件的时间 mtime (Modified time)是文件最后一次写的时间(是在写入 ...
- 用.net installshield打包程序时注册第三方控件
制作打包程序时如果用到外部控件需要按以下方式操作: 1.将控件及控件所用到的所有DLL加入打包程序. 2.将控件的Register由vsdrfDoNotRegister改为vsdrfCOMSelfRe ...
- 基于python实现的DDoS
目录 一个简单的网络僵尸程序 一个简单的DOS攻击程序 整合网络僵尸和DoS攻击--DDoS 代码地址如下:http://www.demodashi.com/demo/12002.html 本例子包含 ...
- 【转】前端上传组件Plupload
[转自博客园-无双] html5原生的给我们提供了文件上传的API,Plupload是一款由著名的web编辑器TinyMCE团队开发的上传组件,简单易用且功能强大,我们完全可以使用Plupload来代 ...
- [DevExpress]XtraTabControl右键加入关闭当前页、关闭其它页、所有关闭的实现
private void xtraTabControl_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButton ...
- cordova开发自己定义插件
以下是自己定义cordova插件的基本入门.做插件的小白可以參考一下哈,兴许会更新插件的进阶博客,希望大家可以共同学习共同进步 1.环境搭建 cordova插件开发前须要安装一些软件和配置环境 1.1 ...
- Centos中配置环境变量
以Java的开发环境Jdk为例. 将jdk-9.0.1放置在/usr/local下(UNIX规范),然后我们将jdk配置到环境变量中去. $ mv jdk- /usr/local $ vim /etc ...
- 【转载】IE下利用滤镜实现背景颜色渐变
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...