Restoring Road Network(Floyd算法的推广)
个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况,
后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧。
题意概述:
就是给定一个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 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
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算法的推广)的更多相关文章
- Restoring Road Network Floyd
问题 C: Restoring Road Network 时间限制: 1 Sec 内存限制: 128 MB提交: 731 解决: 149[提交] [状态] [讨论版] [命题人:admin] 题目 ...
- Restoring Road Network
D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem State ...
- zoj1967 poj2570 Fiber Network (floyd算法)
虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...
- 【AtCoder Beginner Contest 074 D】Restoring Road Network
[链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...
- 【Atcoder】ARC083 D - Restoring Road Network
[算法]图论,最短路? [题意]原图为无向连通图,现给定原图的最短路矩阵,求原图最小边权和,n<=300. [题解]要求最小边权和下,原图的所有边一定是所连两端点的最短路. 那么现在将所有最短路 ...
- AtCoder Regular Contest 083 D: Restoring Road Network
题意 有一张无向带权连通图(点数<=300),给出任意两点i,j之间的最短路长度dis[i][j].问是否存在一张这样的无向图.如果不存在输出-1.如果存在输出所有这样的无向图中边权和最小的一张 ...
- [Arc083D/At3535] Restoring Road Network - 最短路,结论
[Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...
- 图论(floyd算法):NOI2007 社交网络
[NOI2007] 社交网络 ★★ 输入文件:network1.in 输出文件:network1.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在社交网络( ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
随机推荐
- iOS imageNamed VS imageWithContentsOfFile
今天 又学习了 一个 提高应用交互效率 降低内存的 小知识 结论: (1)mageNamed加载图片,并且把image缓存到内存里面, (2)imageWithContentsOfFile是只显示图片 ...
- jpa,jdbc,hibernate/mybatis,数据库驱动
JPA是规范,hibernate/mybatis是对规范的实现,hibernate/mybatis是对jdbc的封装,也就是说hibernate/mybatis还是会调用jdbc. 我们平时使用 ...
- C++ IPv4与IPv6的兼容编码(转,出自http://blog.csdn.net/ligt0610/article/details/18667595)
这里不再对IPv6 socket相关编程的基础知识进行讲解,只提供一个IP协议无关的服务端和客户端的代码,仅供参考. 服务端代码: #include <iostream> #include ...
- 建议47:使用logging记录日志信息
# -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...
- nodejs模块Phantom,无界面浏览器
PhantomJS 是一个无界面的 webkit 内核浏览器,
- unity json解析IPA后续
以前说到的,有很大的限制,只能解析简单的类,如果复杂的就会有问题,从老外哪里看到一片博客,是将类中的list 等复杂对象序列化, using UnityEngine; using System.C ...
- Go 书单
一.<Go语言学习笔记> (未找到对应版本的电子书,大家可以去作者github:https://github.com/qyuhen/book) 推荐理由:作为时下流行的一种系统编程语言,G ...
- string 类(二)
处理string对象中的字符: 在cctype头文件中定义了一组标准库函数来处理string对象中的字符,比如检查一个string对象是否包含空白,或者把string对象中的字母改成小写,再或者查看某 ...
- Variation calling and annotation
Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...
- VCFtools
The C++ executable module examples This page provides usage examples for the executable module. Exte ...