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. html checkbox 实现全选/取消全选

    html checkbox  实现全选/取消全选 <html> <body> <table border="1"> <tr> < ...

  2. 移动负载均衡技术(MBL)

    移动负载均衡技术(MBL)   转至元数据结尾 附件:5 被admin添加,被admin最后更新于四月 27, 2015 转至元数据起始 互联网技术发展到今天,已经进入移动时代,很多在传统CS和BS的 ...

  3. iOS开发-重写description方法,自定义控制台(log)信息

    description是所有类都有的一个方法. 我们重写这个方法,可以自定义实例输出的信息. 比如我们创建一个Person类: 在.h文件中添加两个属性: #import <Foundation ...

  4. node.js 学习01

    PHP开发技术栈(LAMP) Linux  Apache  MySql  PHPnode.js 全栈开发技术栈(MEAN): MongoDB  Express  Angular  Node.js 现阶 ...

  5. 牛散NO.3:MACD放之四海 假作真时真亦假

    大宗商品日线“异曲同工夺命勾魂枪” 话说有实战意义的技术在任何资本市场里都能产生出神奇的效果.不能说放之四海皆准,但至少起到触类旁通的“牵强”吧.大宗商品特别是在国际市场交易的大宗 商品由于是来自各方 ...

  6. Linux 性能监控 —— Load Average

    一. 简单介绍 top. uptime. cat /proc/loadavg 命令中 Load average: 4.90, 5.51, 5.77        整体含义: 正在执行的任务数量 + 排 ...

  7. Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle

    Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq  index2 3.  ...

  8. Android JNI和NDK学习(01)--搭建NDK开发环境(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3095013.html 本文主要介绍“JNI”.“Android NDK”以及 ...

  9. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  10. InputArray和OutputArray

    源码路径:~/opencv-2.4.9/modules/core/include/opencv2/core/core.hpp where _InputArray is a class that can ...