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. map方法,以及filter方法的使用

    map()方法,会返回一个 jQuery 封装的数组, 这个数组不能直接使用,需要使用 get() 来处理返回的对象以得到基础的数组. 例子: <!DOCTYPE html> <ht ...

  2. lua类库 middleclass学习笔记

    middleclass使在lua中面象对象变的简单 抄了一遍他的示例代码运行着试了试,基本懂了 local class = require 'middleclass' --类的继承 Person = ...

  3. 使用dynamic类型来优化反射

    什么是dynamic类型?微软给出的官方文档中这样解释:在通过 dynamic 类型实现的操作中,该类型的作用是绕过编译时类型检查. 改为在运行时解析这些操作. dynamic 类型简化了对 COM ...

  4. 几段表单处理的JQuery代码(转)

    1 只接受数字输入 $("#uAge").keydown(function(event) { // 允许退格和删除键 if ( event.keyCode == 46 || eve ...

  5. VA使用技巧

    Reserved String Meaning Date $DATE$ Year/month/day formatted as %04d/%02d/%02d   $DAY$ Day of month ...

  6. Atitit.跨语言  文件夹与文件的io操作集合  草案

    Atitit.跨语言  文件夹与文件的io操作集合  草案 1. Jdk原生的太难用了..1 2. PS: apache commons-io包,FileUtils有相关的方法,IOUtils一般是拷 ...

  7. INSERT 失败,因为下列 SET 选项的设置不正确: 'ARITHABORT'

    当你在SQL Server上试图更新一个索引视图引用的表时,你可能回收到如下有错误 INSERT 失败,因为下列 SET 选项的设置不正确: 'ARITHABORT' 你必须在TSQL前Set ARI ...

  8. 转:SQL2008 UNPIVOT 列转行示例

    CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int); GO INSERT INTO pv ...

  9. MySQL 汉字转拼音

    一 . fristPinyin : 此函数是将一个中文字符串的第一个汉字转成拼音字母 (例如:"中国人"->Z) )) ) CHARSET utf8 BEGIN ); )), ...

  10. 初识Quartz(二)

    简单作业: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package quartz_pr ...