贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。
问题 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. 20165325《Java程序设计》第九周学习总结

    一.教材学习笔记 ch13 1.URL类 URL类是java.net包中的一个重要的类,URL的实例封装着一个统一资源定位符,使用URL创建对象的应用程序称作客户端程序. 一个URL对象通常包含最基本 ...

  2. SpringSecurity基于数据库RBAC数据模型控制权限

    ⒈通用RBAC(Role - Based Access Control)数据模型 ⒉如何使用 1. package cn.coreqi.ssoserver.rbac; import org.sprin ...

  3. 使用SpringSocial开发微信登录

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

  4. Delphi 实现自动更新

    Delphi 通用程序自动更新升级:http://www.delphitop.com/html/wangluo/2968.html https://www.cnblogs.com/hnxxcxg/p/ ...

  5. php-GatewayWorker搭建实时聊天室

    ├── Applications // 这里是所有开发者应用项目 │ └── YourApp // 其中一个项目目录,目录名可以自定义 │ ├── Events.php // 开发者只需要关注这个文件 ...

  6. VS2017打包C#桌面应用

    原文地址:https://blog.csdn.net/houheshuai/article/details/78518097 在要打包项目的解决方案 右键→添加→ 新建项目 后出现如下选择 如果没有V ...

  7. codeforces 462div.2

    A A Compatible Pair standard input/output 1 s, 256 MB    x1916 B A Prosperous Lot standard input/out ...

  8. c++内存对齐原理

    转载自http://blog.csdn.net/it_yuan/article/details/24651347 #类中的元素 0. 成员变量   1. 成员函数   2. 静态成员变量   3. 静 ...

  9. [jquery]为jQuery.ajax添加onprogress事件

    原理: 给XMLHttpRequest对象的upload属性绑定onprogress方法监听上传过程 var xhr = new XMLHttpRequest();  xhr.upload.onpro ...

  10. 如何去掉li标签的重叠边框

    当我们的li标签设置了border的时候就会出现重叠边框,如何去掉呢,见代码 html代码 <ul class="friendLink_list"> <li> ...