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 ...
随机推荐
- B3:状态模式 State
当一个对象内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决当控制一个对象状态转换条件表达式过于复杂时的情况,把状态判断逻辑移到表示不同状态的一系列类中.如果状态判断很简单, ...
- 在LoadRunner中设置HTTP请求time-out的时间
Error -27728: Step download timeout (120 seconds) has expired when downloading non-resource(s) [MsgI ...
- C#运用反射调用其他程序集中的代码
加载程序集 AssMedicalAdvice = Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, "Inscript ...
- 基于环信的仿QQ即时通讯的简单实现
代码地址如下:http://www.demodashi.com/demo/11645.html 我的博客地址 之前一直想实现聊天的功能,但是感觉有点困难,今天看了环信的API,就利用下午的时间动手试了 ...
- 神奇的canvas——巧用 canvas 为图片添加水印
代码地址如下:http://www.demodashi.com/demo/11637.html 很久之前写过一篇关于 canvas 的文章,是通过 canvas 来实现一个绚丽的动画效果,不管看过没看 ...
- Log4j学习(使用流程,Log4j.properties配置文件,配置到多个不同目标)
参考网址: [http://blog.csdn.net/drift_away/article/details/7403658] [http://blog.csdn.net/lxzo123/articl ...
- jquery的val()
jQuery 属性操作 - val() 方法 jQuery 属性操作参考手册 实例 设置输入域的值: $("button").click(function(){ $(": ...
- Python 中,matplotlib绘图无法显示中文的问题
在python中,默认情况下是无法显示中文的,如下代码: import matplotlib.pyplot as plt # 定义文本框和箭头格式 decisionNode = dict(boxsty ...
- Java并发编程(二)为什么需要多线程
如果不考虑多线程的话,那么在程序只有一条执行路径,代码串行执行:顺序执行.选择或者循环.单线程就像你用你惯常的手去写字,多线程编程就要求你左手画圆,右手画方.一不留神就会手忙脚乱,圆不是圆,方也不像方 ...
- Python版***R设置后台自启动的方法
---恢复内容开始--- shadowsocks客户端中没有自带daemon的启动方法.需要通过自己的设置来实现自启动和后台管理. 在Ubuntu 15.04之后,systemd代替了upstart成 ...