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 ij1≤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

Copy
3
0 1 3
1 0 2
3 2 0

Sample Output 1

Copy
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

Copy
3
0 1 3
1 0 1
3 1 0

Sample Output 2

Copy
-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

Copy
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

Copy
82

Sample Input 4

Copy
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的更多相关文章

  1. Restoring Road Network Floyd

    问题 C: Restoring Road Network 时间限制: 1 Sec  内存限制: 128 MB提交: 731  解决: 149[提交] [状态] [讨论版] [命题人:admin] 题目 ...

  2. Restoring Road Network(Floyd算法的推广)

    个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况, 后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧. 题意概述: 就 ...

  3. AtCoder Regular Contest 083 D: Restoring Road Network

    题意 有一张无向带权连通图(点数<=300),给出任意两点i,j之间的最短路长度dis[i][j].问是否存在一张这样的无向图.如果不存在输出-1.如果存在输出所有这样的无向图中边权和最小的一张 ...

  4. 【Atcoder】ARC083 D - Restoring Road Network

    [算法]图论,最短路? [题意]原图为无向连通图,现给定原图的最短路矩阵,求原图最小边权和,n<=300. [题解]要求最小边权和下,原图的所有边一定是所连两端点的最短路. 那么现在将所有最短路 ...

  5. 【AtCoder Beginner Contest 074 D】Restoring Road Network

    [链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...

  6. [Arc083D/At3535] Restoring Road Network - 最短路,结论

    [Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...

  7. LiDAR Textbook & Automated Road Network Extraction

    Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...

  8. 据说是Flord算法

    贵有恒,何必三更起五更眠:最无益,莫过一日曝十日寒. 问题 C: Restoring Road Network 问题 C: Restoring Road Network 时间限制: 1 Sec  内存 ...

  9. 【AtCoder】ARC083

    C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...

随机推荐

  1. $HTTP_RAW_POST_DATA 与$_POST

    出处:http://blog.163.com/gwo-cce@126/blog/static/325736492008101142422345/ 这是手册里写的 总是产生变量包含有原始的 POST 数 ...

  2. 配置git账号和密码

    最开始启动的时候 配置用户名和用户邮箱,安装完git第一件要做的事情! git config --global uesr.name "Sunnshino" git config - ...

  3. 病毒木马查杀实战第022篇:txt病毒研究

    前言 反病毒爱好者们非常喜欢讨论的一个问题就是,现在什么样的病毒才算得上是主流,或者说什么样的病毒才是厉害的病毒呢?我们之前的课程所解说的都是Ring3层的病毒.所以有些朋友可能会觉得.那么Ring0 ...

  4. C语言循环中降低推断——————【Badboy】

    为了让编译器更好地优化循环,应该尽量让循环中降低推断,方法之中的一个是将推断语句整合进表达式.还是这个样例: for (int i = 0; i < 1000*10; i++) { sum += ...

  5. iOS开发-XCode常用快捷键整理

    前言:如果我们能够掌握并巧妙地使用快捷键,可以大大加快我们的工作效率,这个对经常使用快捷键的人们来说,应该很容易理解.因此我们需要做的是,针对于自己经常使用的快捷键去进行记忆.我不会推荐你们去把所有的 ...

  6. 《windows核心编程》 17章 内存映射文件

    内存映射文件主要用于以下三种情况: 系统使用内存映射文件载入并运行exe和dll,这大量节省了页交换文件的空间以及应用程序的启动时间 开发人员可以使用内存映射文件来访问磁盘上的数据文件.这使得我们可以 ...

  7. 02-4设置第一启动项--U盘装系统中bios怎么设置USB启动

    整个U盘启动里最关键的一步就是设置U盘启动了,本教程内只是以特定型号的电脑为例进行演示,鉴于各种电脑不同BIOS设置U盘启动各有差异,所以如果下面的演示不能适用于你的电脑,建议去百度或者谷歌搜索一下你 ...

  8. filebeat.service

    # # filebeat systemd service # [Unit] Description=Filebeat Documentation=https://www.elastic.co/guid ...

  9. ORCAD应用小技巧

    1.位号复制 递增和不变 2.netlist log信息 3.off page页码对其 4.批量修改off page符号命名的方法 5.批量修改电源符号 6.capture中instance和Occu ...

  10. 关于http和rpc的区别(segmentfault上的回答)

    问题最近用了谷歌的grpc,所以对rpc和http有一点疑惑,感觉这两个东西功能上是一样的,rpc某个服务监听某一个方法,客户端调用这个方法,返回相应的数据,和http监听某个方法的路由 返回相应的数 ...