贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。
问题 C: Restoring Road Network

问题 C: Restoring Road Network

时间限制: 1 Sec  内存限制: 128 MB
提交: 896  解决: 184
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

输入

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


输出

If there exists no network that satisfies the condition, print -1. If it exists, print the shortest possible total length of the roads.

样例输入

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算法的更多相关文章

  1. hdu 1217 利用flord算法求 有环图 2点之间最大值

    Arbitrage                                                      T ime Limit: 2000/1000 MS (Java/Other ...

  2. Flord算法传递闭包

    POJ3660 对于flord算法得学习,这篇博客写的非常好http://blog.csdn.net/ljhandlwt/article/details/52096932 这个题问你给你n头牛得前后关 ...

  3. 单源最短路径——Floyd算法

    正如我们所知道的,Floyd算法用于求最短路径.Floyd算法可以说是Warshall算法的扩展,三个for循环就可以解决问题,所以它的时间复杂度为O(n^3). Floyd算法的基本思想如下:从任意 ...

  4. OPTICS光学算法

    package com.my.optics; public class DataPoint { private String name;//样本的名字 private double dimensioi ...

  5. 关于O(n)算法

    首先要明确一点,当数据规模达到百万时需用O(n)算法 如何实现O(n)算法,其实是对原有算法的一种改进 后者说是 原有算法+一点小性质=O(n)算法 下面我将举几个例子来说明这一点: 1.后缀数组中h ...

  6. 一步步学算法(算法分析)---6(Floyd算法)

    Floyd算法 Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命 ...

  7. 【转】AC算法详解

    原文转自:http://blog.csdn.net/joylnwang/article/details/6793192 AC算法是Alfred V.Aho(<编译原理>(龙书)的作者),和 ...

  8. 模拟Paxos算法及其简单学习总结

    一.导读 Paxos算法的流程本身不算很难,但是其推导过程和证明比较难懂.在Paxos Made Simple[1]中虽然也用了尽量简化的流程来解释该算法,但其实还是比较抽象,而且有一些细节问题没有交 ...

  9. python Kmeans算法解析

    一. 概述 首先需要先介绍一下无监督学习,所谓无监督学习,就是训练样本中的标记信息是位置的,目标是通过对无标记训练样本的学习来揭示数据的内在性质以及规律.通俗得说,就是根据数据的一些内在性质,找出其内 ...

随机推荐

  1. dubbo学习笔记1

    DUBBO入门 官方文档:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/ 服务提供者 项目结构: pom文件: <?xml ...

  2. 使用SpringSocial开发微信登录

    ⒈编写微信用户对应的数据结构 package cn.coreqi.social.weixin.entities; /** * 微信用户实体类 */ public class WeixinUserInf ...

  3. SpringSecurity实现图形验证码功能

    ⒈封装验证码类 package cn.coreqi.security.validate; import java.awt.image.BufferedImage; import java.time.L ...

  4. OpenCV 入门

    1.入门攻略[安装用] https://www.cnblogs.com/linshuhe/p/5764394.html 2.VS2017配置opencv教程(超详细!!!) https://blog. ...

  5. 【Linux】虚拟服务器之LVS

    写在前面 觉得甚是幸运,能够有机会参与到ITOO配置环境的工作中去.现在正在熟悉,在搭建环境的时候,有LVS安装配置教程,对这一块有些懵逼,这几天查了一些资料,写在这里,和大家分享一下 是什么 LVS ...

  6. Ubuntu 16.04 Matlab2015b安装

    小白一个,安装过程参考了一大堆教程. 这里记录一下. 一.安装 1) sudo mkdir /media/matlab 2) cd 到下载的镜像文件所在文件夹 3) 挂载镜像: sudo mount ...

  7. k64 datasheet学习笔记50---GPIO

    1.前言 GPIO模块支持8bit 16bit 32bit访问,当被配置为GPIO功能时,GPIO数据方向和数据寄存器控制了每个引脚的输出方向和输出数据 不管GPIO配置为何种功能,GPIO输入寄存器 ...

  8. CF449C:Jzzhu and Apples

    题意简述 给出正整数n,你要把1-n之间的正整数分成尽可能多组,使得每一组两个数的最大公约数大于1;输出能分成最多组的个数,并按任意顺序输出每组的两个数. 很妙的一道题. 首先我们考虑去处理每个质数的 ...

  9. MinGW-w64非官方编译器集成安装包(很清楚)

    官方链接[编辑] MinGW 官方网站. SourceForge.net上的MinGW官方的下载站点(Win32系统). MinGW-w64官方的下载站点[编辑] MinGW-w64是2005年由On ...

  10. VBS学习

    简介 VBS windows脚本 Microsoft visual basic script editionVBS 是visual basic的一个抽象子集,系统内置,不编译为二进制文件,(由host ...