据说是Flord算法
问题 C: Restoring Road Network
问题 C: Restoring Road Network
时间限制: 1 Sec 内存限制: 128 MB
提交: 896 解决: 184
[提交] [状态] [讨论版] [命题人: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.
这个题目一开始没读懂,明明是最短路了为啥还要最短路,赛后查题解才知道,原来是需要你进行判断。。。。
自己为什么这么菜。注意 he thought !!!!
判断的时候 如果 经过一个额外的一点的距离小于所给的数据,那么他就不是最短距离。
#include <iostream>
using namespace std;
int a[310][310];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int flag=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(a[i][k]+a[k][j]<a[i][j]&&i!=j&&j!=k&&i!=k){
flag=1;
break;
}
if(a[i][k]+a[k][j]==a[i][j]&&i!=j&&j!=k&&i!=k){
a[i][k]=0;//这里不应该赋值为0,会影响之后的判断。
a[k][i]=0;
}
}
if(flag) {
break;
}
}
if(flag){
break;
}
}
if(flag){
cout<<-1;
}
else{
int sum=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
sum+=a[i][j];
+1;j<n;j++){
if(v[i][j]!=1){`}
sum+=a[i][j];
}
}
}
cout<<sum;
}
}
}
}
cout<<sum;
据说是Flord算法的更多相关文章
- hdu 1217 利用flord算法求 有环图 2点之间最大值
Arbitrage T ime Limit: 2000/1000 MS (Java/Other ...
- Flord算法传递闭包
POJ3660 对于flord算法得学习,这篇博客写的非常好http://blog.csdn.net/ljhandlwt/article/details/52096932 这个题问你给你n头牛得前后关 ...
- 单源最短路径——Floyd算法
正如我们所知道的,Floyd算法用于求最短路径.Floyd算法可以说是Warshall算法的扩展,三个for循环就可以解决问题,所以它的时间复杂度为O(n^3). Floyd算法的基本思想如下:从任意 ...
- OPTICS光学算法
package com.my.optics; public class DataPoint { private String name;//样本的名字 private double dimensioi ...
- 关于O(n)算法
首先要明确一点,当数据规模达到百万时需用O(n)算法 如何实现O(n)算法,其实是对原有算法的一种改进 后者说是 原有算法+一点小性质=O(n)算法 下面我将举几个例子来说明这一点: 1.后缀数组中h ...
- 一步步学算法(算法分析)---6(Floyd算法)
Floyd算法 Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命 ...
- 【转】AC算法详解
原文转自:http://blog.csdn.net/joylnwang/article/details/6793192 AC算法是Alfred V.Aho(<编译原理>(龙书)的作者),和 ...
- 模拟Paxos算法及其简单学习总结
一.导读 Paxos算法的流程本身不算很难,但是其推导过程和证明比较难懂.在Paxos Made Simple[1]中虽然也用了尽量简化的流程来解释该算法,但其实还是比较抽象,而且有一些细节问题没有交 ...
- python Kmeans算法解析
一. 概述 首先需要先介绍一下无监督学习,所谓无监督学习,就是训练样本中的标记信息是位置的,目标是通过对无标记训练样本的学习来揭示数据的内在性质以及规律.通俗得说,就是根据数据的一些内在性质,找出其内 ...
随机推荐
- ffmpeg笔记
1.视频降低质量,减小体积: ffmpeg -i aaa.mp4 -strict -2 -qscale 20 -y outfile.mp4
- 学习基础和C语言基础调查
学习基础和C语言基础调查 一.一种比大多数人(超过90%以上)更好的技能 说句实话,我还没有那种特别特别自信的.说我能拿得出手的.所谓能超过百分之九十以上的人的技能.毕竟人外有人,天外有天. 姑且算上 ...
- log4j - 输出格式控制, PatternLayout参数含义以及详细配置
转载自:https://blog.csdn.net/reserved_person/article/details/52849505 做项目被log4j的输出格式化参数搞烦了,索性把API的相关部分大 ...
- JavaScript-client、offset、scroll、定时器
client offset scroll client.offset.scroll系列 他们的作用主要与计算盒模型,盒子的偏移量和滚动有关 clientTop 内容区域到边框顶部的距离, 说白了, 就 ...
- 《Debug Hacks》和调试技巧【转】
转自:https://blog.csdn.net/sdulibh/article/details/46462529 Debug Hacks 作者为吉冈弘隆.大和一洋.大岩尚宏.安部东洋.吉田俊辅,有中 ...
- Windows PowerShell 入門(9)-エラー編
対象読者 Windows PowerShellでコマンドレット操作ができる方 何らかのプログラミング経験があればなお良い 必要環境 Windows PowerShell エラーをリダイレクトする リダ ...
- ARX工程必须使用release模式编译
在添加如下代码保证debug版本的arx文件也是使用MFC的release库 // 'DEBUG workaround' below prevents the MFC or ATL #includ ...
- Linux中OCI开发库的配置
Oracle调用接口(Oracle Call Interface,简称OCI)提供了一组可对Oracle数据库进行存取的接口子例程(函数),通过在第三代程序设计语言(如C语言)中进行调用可达到存取Or ...
- 洛谷 P4948 拉格朗日多项式插值(杜老师板子)
https://www.luogu.org/problemnew/show/P4948 这篇博客主要目的是存一下的dls的神奇板子,本来应该是推公式或者二分做的 但是dls的插值板子直接写好了这个特殊 ...
- python迭代器的说明
data = [randint(0,20) for _ in xrange(30)]表示30个随机生成的0-20随机数其中for _ in xrange(30)表示循环30次. from random ...