个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况,

后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧。

题意概述:

就是给定一个N阶方阵,规定Auv,为u到v的最短路径,若给出的数据存在其他通路少于此时的值则不存在即为假,

解决方法就是利用Floyd算法进行单源最短路的判断,只要后面的矩阵与原来的不相符就是假的。真的的时候,是要求

存在的最短总路程使得矩阵的数成立,我画了下就是只要存在从其他城市能够转到目的地的时候就可以不要这条直接到达的

路,当时脑袋短路没有弄出来,后面一想只要对于每条路进行判断,若存在这样的路就不加在sum里面就好了

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 ij, 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

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
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
int t,n;
long long dis[][];
long long d[][];
void init()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dis[i][j]=d[i][j];
}
bool panduan()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(dis[i][j]!=d[i][j]) return false;
return true;
}
long long sum()
{
long long s=,fond;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
fond=;
for(int k=;k<=n;k++){
if(k==i||k==j) continue;
if(dis[i][j]==dis[i][k]+dis[k][j])
fond=;
}
if(fond) s+=dis[i][j];
} return s;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>d[i][j];
init();
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(d[i][j]>d[i][k]+d[k][j])
d[i][j]=d[i][k]+d[k][j];
int t=panduan();
if(!t) cout<<"-1"<<endl;
else cout<<sum()<<endl;
return ;
}

Restoring Road Network(Floyd算法的推广)的更多相关文章

  1. Restoring Road Network Floyd

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

  2. Restoring Road Network

    D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem State ...

  3. zoj1967 poj2570 Fiber Network (floyd算法)

    虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...

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

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

  5. 【Atcoder】ARC083 D - Restoring Road Network

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

  6. AtCoder Regular Contest 083 D: Restoring Road Network

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

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

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

  8. 图论(floyd算法):NOI2007 社交网络

    [NOI2007] 社交网络 ★★   输入文件:network1.in   输出文件:network1.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 在社交网络( ...

  9. POJ 1502 MPI Maelstrom(模板题——Floyd算法)

    题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...

随机推荐

  1. mysql 批量更新多条记录(且不同值)的实现方法

    mysql更新语句很简单,更新多条数据的某个字段为相同值,一般这样写: UPDATE table_name SET field = 'value' WHERE condition; 更新多条数据为不同 ...

  2. 计算机网络概述---OSI参考模型

    应用层:所有能产生网络流量的程序,例如:qq等,txt记事本没有产生流量,所以不属于应用层: 表示层:在传输之前对应用层的数据进行加工或处理,例如:加密.压缩.传视频时二进制,传文档时ASCII码 等 ...

  3. WebSocket 初体验

    其实老早就觊觎 Socket 这碗美食了,在 WebSocket 发出后更是心潮澎湃... 奈何这需要后端同志的帮助,使得至今才得以品尝.(当然本文也只涉及前端部分) 以前想监听其他设备变化,大屏幕交 ...

  4. IMX6Q RTC驱动分析

    对于在工作中学习驱动的,讲究的是先使用,再理解.好吧,我们来看看板子里是如何注册的? 在板文件里,它的注册函数是这样的: imx6q_add_imx_snvs_rtc() 好吧,让我们追踪下去: 1 ...

  5. 笔记:git和码云

    背景:之前使用GitHub,无奈网速原因,有时候竟无法连接,搜索解决方案而又鱼龙混杂淹没在信息的海洋. 于是尝试码云,界面简单,全中文,用起来很是顺手. 码云使用git来管理,操作上都是git的基本指 ...

  6. INSPIRED启示录 读书笔记 - 第21章 产品验证

    证明产品的价值.可用性.可行性 产品验证是指在正式开发.部署产品前,验证产品说明文档描述的产品是否符合预期要求 产品经理向产品团队提供最终的产品说明文档前,需要进行三项重要验证 1.可行性测试:明确在 ...

  7. Kubernetes Metrics-Server

    github地址:https://github.com/kubernetes-incubator/metrics-server 官网介绍:https://kubernetes.io/docs/task ...

  8. windows10 搜索桌面搜索功能失效的解决

    windows桌面的搜索框用起来很方便,很多时候直接把不常用的程序的快捷方式删掉,直接从搜索框搜索就可以,但是这两天突然不能用了,今天晚上找了一下原因,终于弄好了. 参考知乎上面的陈滔滔的方法: ht ...

  9. MySQL之——提示"mysql deamon failed to start"错误的解决方法

    网站突然连接不上数据库,于是直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon failed to start&q ...

  10. P2610 【[ZJOI2012]旅游】(dfs+树的直径)

    楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图 ...