Restoring Road Network Floyd
问题 C: Restoring Road Network
时间限制: 1 Sec 内存限制: 128 MB
提交: 731 解决: 149
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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
输入
N
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
…
AN,1 AN,2 … AN,N
输出
样例输入
3
0 1 3
1 0 2
3 2 0
样例输出
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.
题意:给出一个N * N的邻接矩阵,判断这个矩阵是否是跑过floyd的,即任意两点都不可以被松弛,如果可以松弛,输出-1,否则求所有路径唯一(只能直接到达)的两点之间的距离和
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll g[][];
int main()
{
int n;
cin>>n;
ll ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)cin>>g[i][j];
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
{
bool flag=true;
for(int k=;k<=n;k++)
{
if(g[i][j]>g[i][k]+g[k][j])
{
cout<<-<<endl;
return ;
}
if(i!=k&&j!=k&&g[i][j]==g[i][k]+g[k][j])flag=false;//两点之间路径不唯一的不计入ans
}
if(flag)ans+=g[i][j];
}
cout<<ans<<endl;
return ;
}
Restoring Road Network Floyd的更多相关文章
- Restoring Road Network
D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem State ...
- Restoring Road Network(Floyd算法的推广)
个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况, 后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧. 题意概述: 就 ...
- 【AtCoder Beginner Contest 074 D】Restoring Road Network
[链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...
- AtCoder Regular Contest 083 D: Restoring Road Network
题意 有一张无向带权连通图(点数<=300),给出任意两点i,j之间的最短路长度dis[i][j].问是否存在一张这样的无向图.如果不存在输出-1.如果存在输出所有这样的无向图中边权和最小的一张 ...
- 【Atcoder】ARC083 D - Restoring Road Network
[算法]图论,最短路? [题意]原图为无向连通图,现给定原图的最短路矩阵,求原图最小边权和,n<=300. [题解]要求最小边权和下,原图的所有边一定是所连两端点的最短路. 那么现在将所有最短路 ...
- [Arc083D/At3535] Restoring Road Network - 最短路,结论
[Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...
- LiDAR Textbook & Automated Road Network Extraction
Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...
- find the safest road(floyd)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission ...
- zoj1967 poj2570 Fiber Network (floyd算法)
虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...
随机推荐
- CSS:CSS 尺寸 (Dimension)
ylbtech-CSS:CSS 尺寸 (Dimension) 1.返回顶部 1. CSS 尺寸 (Dimension) CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允 ...
- This inspection detects shadowing names defined in outer scopes.
错误信息:This inspection detects shadowing names defined in outer scopes. 检查到波浪处的单词已在函数外部定义. 解决:使用global ...
- C# 中如何输出双引号(转义字符的使用)
实现效果: 输出这样的一个含有双引号的字符串 "hello" 方式一: 不用 @ 时转义 System.Console.WriteLine("\"he ...
- 第十五章 例行性工作(crontab)--循环执行的例行性工作调度 crontab(定时任务)
循环执行的例行性工作调度 crontab(定时任务) 15.1 例行性工作调度 不考虑硬件与服务器的链接状态,Linux帮助提醒很多任务. Linux例行性工作是如何进行调度的? Linux调度就是通 ...
- Apache配置 PHP 支持
1,在服务区安装PHP 解压 php 到纯英文路径目 2,添加 PHP处理模块 LoadModule php7_module C:/ProgramData/php/php7apache2_4.dl 3 ...
- Rsync 恢复 libselinux.SO.1
libselinux.SO.1 这个文件对 CentOS 7很重要, 误删掉后,会导致很多命令无法使用(比如yum ,rpm 命令),利用rsync这个工具来修复. 服务端执行如下配置:(选取正常 ...
- MySQL 8.0及以上版本设置密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- 总分 Score Inflation
题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...
- ForEach控制器
ForEach一般是和用户定义的变量结合使用的 前面前缀一般是和用户定义的变量前缀一致 输出的变量是要使用的变量 这样能保证每次使用的变量不一样 add"_"before numb ...
- C#异步编程----async和await组合的写法
微软示例: private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync ...