问题 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. python 多设备同时安装app包

    python  多设备同时安装app包 上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- import os import time from m ...

  2. 网页压缩--gzip和deflate的区别

    我们在配置网站GZip压缩的时候,会发现有两个模块可以设置的,一个是GZip模块的参数配置,另一个是Deflate模块的参数配置,他们的设置方法是一样的.刚开始时我不太明白,这两地方有什么不同?网站开 ...

  3. 力扣算法题—111.Minimum Depth of Binary Tree

      Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...

  4. 剑指offer——56在排序数组中查找数字

    题目描述 统计一个数字在排序数组中出现的次数.   题解: 使用二分法找到数k然后向前找到第一个k,向后找到最后一个k,即可知道有几个k了 但一旦n个数都是k时,这个方法跟从头遍历没区别,都是O(N) ...

  5. Straight Master (贪心)

    题目如下:A straight is a poker hand containing five cards of sequential rank, not necessarily to be the ...

  6. Unity3D中画拉选框(绘制多选框)

    问题分析: 需要根据鼠标事件,摁下鼠标开始绘制选择框,抬起鼠标结束绘制. 实现思路: 该需求是屏幕画线,Unity内置了GL类  封装了OpenGL,可以通过GL类来实现一些简单的画图操作,这里也是使 ...

  7. Vue之获取用户当前所在省市

    今天小编给大家带来的是使用Vue获取用户所在城市,Vue是很强大的,给大家准备好现成的插件供大家调用,下面的Demo小编使用的是百度API. 首先我们从百度平台申请百度地图的秘钥,申请成功后我们将&l ...

  8. 笔记57 Mybatis和Hibernate的比较

    一.Hibernate和MyBatis简介 1.Hibernate简介 Hibernate对数据库结构提供了较为完整的封装,Hibernate的O/R Mapping实现了POJO 和数据库表之间的映 ...

  9. 干货满满!10分钟看懂Docker和K8S

    2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫“dotCloud”的公司. 这家公司主要提供基于PaaS的云计算技术服务.具体来说,是和LXC有关的容器技术. LXC,就是Linux容器虚 ...

  10. cut sort uniq wc 一 文本处理工具

    cut cut是一个选取命令,就是将一段数据经过分析,取出我们想要的. 一般来说,选取信息通常是针对"行"来进行分析的,并不是整篇信息分析的. -c : 以字符为单位进行分割. c ...