问题 C: Restoring Road Network

时间限制: 1 Sec  内存限制: 128 MB
提交: 731  解决: 149
[提交] [状态] [讨论版] [命题人: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.

[提交][状态]

题意:给出一个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的更多相关文章

  1. Restoring Road Network

    D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem State ...

  2. Restoring Road Network(Floyd算法的推广)

    个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况, 后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧. 题意概述: 就 ...

  3. 【AtCoder Beginner Contest 074 D】Restoring Road Network

    [链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...

  4. AtCoder Regular Contest 083 D: Restoring Road Network

    题意 有一张无向带权连通图(点数<=300),给出任意两点i,j之间的最短路长度dis[i][j].问是否存在一张这样的无向图.如果不存在输出-1.如果存在输出所有这样的无向图中边权和最小的一张 ...

  5. 【Atcoder】ARC083 D - Restoring Road Network

    [算法]图论,最短路? [题意]原图为无向连通图,现给定原图的最短路矩阵,求原图最小边权和,n<=300. [题解]要求最小边权和下,原图的所有边一定是所连两端点的最短路. 那么现在将所有最短路 ...

  6. [Arc083D/At3535] Restoring Road Network - 最短路,结论

    [Arc083D/At3535] 有 \(N\) 个城市,城市与城市之间用长度为整数的无向道路连接. 现有一考古学家找到了一张 \(N×N\) 的表 \(A\) ,这张表代表了这 \(N\) 座城市两 ...

  7. LiDAR Textbook & Automated Road Network Extraction

    Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...

  8. find the safest road(floyd)

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  9. zoj1967 poj2570 Fiber Network (floyd算法)

    虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...

随机推荐

  1. js分割url提取参数

    //分割url提取参数 var url = Window.location.search;//获取url地址?至结尾的所有参数 //key(需要检错的键) url(传入的需要分割的url地址) fun ...

  2. fatal error C1047: 对象或库文件“.\x64\Release\Des.obj”是使用比创建其他对象所用编译器旧的编译器创建的;请重新生成旧的对象和库

    问题描述: 在把一个32位的dll编译成64位的时候提示上面的错误 解决办法: >属性->常规->项目默认值->全程序优化  将这里的默认项 "使用链接时间代码生成& ...

  3. 配置ssh连接会话复用免密码登录

    我们经常使用ssh连接远程主机,为了方便,避免每次登录输入密码,通常使用密钥登录.如果没有设置密钥, 则需要使用密码登录了,若每次都输入密码则十分繁琐.我们可以设置ssh连接会话复用,则登录成功后,会 ...

  4. Django(九) xadmin全局配置

    xadmin的使用,首先需要对model进行注册,才能在后台管理中进行操作. 1.在app里创建py文件:adminx(必须这个名称) 2.导入xadmin和models里的类,格式如下: 其中lis ...

  5. velocity 相关

    Apache Velocity 是一个基于java的模板引擎(template engine) 应用场景1.Web 应用:开发者在不使用 JSP 的情况下,可以用 Velocity 让 HTML 具有 ...

  6. AXD 的使用以及源代码说明

    汇编源代码说明 ;=============================================================================== ;  引用头文件 ;= ...

  7. 简单三层BP神经网络学习算法的推导

    博客园不支持数学公式orz,我也很绝望啊!

  8. Google 公司的代码规范

    如题: C++ , Objective-C, Java, Python, R, Shell, HTML/CSS, JavaScript, AngularJS, Common Lisp ,Vimscri ...

  9. sqlmap结合burpsuite对post请求进行注入测试

    1. 浏览器打开目标地址 http://testasp.vulnweb.com/Login.asp 2. 配置burp代理(127.0.0.1:8080)以拦截请求 3. 点击login表单的subm ...

  10. elementUI+JS实现全选与反选

    在实际项目开发过程中,遇到的需求,需要实现全选以及取消全选等功能,主要使用ElementUI + JS来实现,具体代码如下: <!DOCTYPE html> <html lang=& ...