贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。
问题 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. SpringSecurity权限表达式

    * 当我们想要使用多个权限表达式的时候,是不能直接级联调用的,也就是说,我们只能手写了. @Override protected void configure(HttpSecurity http) t ...

  2. 【VMware vSphere】vSphere Data Protection简介

    [前言] 还记得一月份左右的时候,万达这边的服务器突然宕机,导致所有的项目不得不停止不说,还损失掉了很多宝贵的数据.为了防止这种情况再次发生,所以近期研究了vSphere Data Protectio ...

  3. 【ARTS】01_18_左耳听风-20190311~20190317

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  4. BIM 3D 数据交换格式 ----张建平(清华女)

    1.collada   EXPORTER 2.FBX    3D MAX 3.DAE 4.3D中的OBJ文件格式详解 (  http://www.cnblogs.com/slysky/p/408130 ...

  5. 快速开发工具:Servoy

    快速开发工具:Servoy https://servoy.com/

  6. 最长增长子序列(LIS)

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  7. PHP JSON 数据解析代码

    作者: 字体:[增加减小] 类型:转载   PHP解析JSON 数据代码,与大多数流行的 Web 服务如 twitter .人人网通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据 ...

  8. 配置percona mysql server 5.7基于gtid主主复制架构

    配置mysql基于gtid主主复制架构 环境: 操作系统 centos7. x86_64 mysql版本:Percona-Server-- 测试环境: node1 10.11.0.210 node2 ...

  9. centos系统初始化配置

    .改主机名: sed -i 's#HOSTNAME=.*#HOSTNAME=u05mix06.yaya.corp#g' /etc/sysconfig/network && hostna ...

  10. zepplin0.7.2报错ERROR, exception: null, result: %text java.lang.NullPointerException的处理

    zepplin0.7.2报错ERROR, exception: null, result: %text java.lang.NullPointerException的处理 问题描述: 使用zeppli ...