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算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...
随机推荐
- delete 和 splice 删除数组中元素的区别
delete 和 splice 删除数组中元素的区别 ` var arr1 = ["a","b","c","d"]; d ...
- Java-Class-@I:io.swagger.annotation.ApiOperation
ylbtech-Java-Class-@I:io.swagger.annotation.ApiOperation 1.返回顶部 2.返回顶部 1. package com.ylbtech.api. ...
- MarkDown 快速开始 基础教学
# MarkDown 快速上手 # > [源代码](https://www.cnblogs.com/qiyuexin/p/9932941.html) > by qyx@2018/11/07 ...
- Redis和SpringBoot整合RedisUtils类
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 人人开源打包jar
先在renren-security执行mvn clean install 再到renren-admin目录下,执行mvn clean package,就可以打成renren-admin.jar 如果想 ...
- Promise 的深度学习
1.Promise 是什么? Promise 是异步编程的一种解决方案,比传统的解决方案–回调函数和事件--更合理和更强大.Promise ,简单说就是一个容器,里面保存着某个未来才回结束的事件(通常 ...
- springcloud中config启动时候报错Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"
-noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jm ...
- VUE 中 使用 iview Form组件 enter键防止页面刷新
<Form :label-width="100" inline label-position='left' @keydown.native.enter.prevent =&q ...
- C语言指针变量的长度
#include <stdio.h> int main() { /********************************************* * * 指针的长度:不同机器可 ...
- D3.js(v3)+react 制作 一个带坐标与比例尺的散点图 (V3版本)
上一章做了柱形图,https://www.cnblogs.com/littleSpill/p/10835041.html 这一章做散点图. 散点图(Scatter Chart),通常是一横一竖 ...