zoukankan      html  css  js  c++  java
  • Restoring Road Network

    Restoring Road Network

    时间限制: 1 Sec  内存限制: 128 MB

    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.

    来源/分类

    ABC074&ARC083 


    #include<bits/stdc++.h>
    using namespace std;
    long long Map[350][350];
    int vis[350][350]={0};
     
    int main()
    {
        int n;
        long long ans=0;
     
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)scanf("%lld",&Map[i][j]);
     
    
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            
        int t=1;
        
        for(int k=1;k<=n;k++)
        if(i!=j&&i!=k&&j!=k&&Map[i][j]>Map[i][k]+Map[k][j])
        {
        printf("-1");
        return 0;
        }
        else
        if(i!=j&&i!=k&&j!=k&&Map[i][j]==Map[i][k]+Map[k][j])t=0;
        
        
        if(t)ans+=Map[i][j];
    
          }
     
     
       printf("%lld",ans/2);
       return 0;
    }
    View Code

    为什么这里的循环和floyd算法里的不一样,我个人认为是因为我们现在要检测每两个点之间是否是最短路状态,所以我们枚举其他点,看看是否能进行最小路径的更新工作,也就是“松弛”操作。

    至于第二问,我们要想求建立这个图的最小边花费,就要尽可能的重用边,话句话说,就是如果A->B->C和A->C的路径花费一样,我们肯定选第一条路径。

    又因为这样选的边是双向的,所以答案要除以2。


  • 相关阅读:
    dockerk个人学习(0)
    ubuntu编译python源码的坑
    查找大目录
    ubuntu 远程gui显示
    paramiko模块
    python open和file的区别
    python type metaclass
    python 生成器 迭代器 yiled
    博客暂停更新,请移步新主页
    win10禁用自动更新服务
  • 原文地址:https://www.cnblogs.com/tian-luo/p/9411133.html
Copyright © 2011-2022 走看看