zoukankan      html  css  js  c++  java
  • Restoring Road Network(Floyd算法的推广)

    个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况,

    后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧。

    题意概述:

    就是给定一个N阶方阵,规定Auv,为u到v的最短路径,若给出的数据存在其他通路少于此时的值则不存在即为假,

    解决方法就是利用Floyd算法进行单源最短路的判断,只要后面的矩阵与原来的不相符就是假的。真的的时候,是要求

    存在的最短总路程使得矩阵的数成立,我画了下就是只要存在从其他城市能够转到目的地的时候就可以不要这条直接到达的

    路,当时脑袋短路没有弄出来,后面一想只要对于每条路进行判断,若存在这样的路就不加在sum里面就好了

    Problem Statement

    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 ij, 1≤Ai,j=Aj,i≤109.
    • Ai,i=0

    Inputs

    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
    

    Outputs

    If there exists no network that satisfies the condition, print -1. If it exists, print the shortest possible total length of the roads.


    Sample Input 1

    Copy
    3
    0 1 3
    1 0 2
    3 2 0
    

    Sample Output 1

    Copy
    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.

    Sample Input 2

    Copy
    3
    0 1 3
    1 0 1
    3 1 0
    

    Sample Output 2

    Copy
    -1
    

    As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3. However, according to the table, the shortest distance between City 1 and City 3 must be 3.

    Thus, we conclude that there exists no network that satisfies the condition.


    Sample Input 3

    Copy
    5
    0 21 18 11 28
    21 0 13 10 26
    18 13 0 23 13
    11 10 23 0 17
    28 26 13 17 0
    

    Sample Output 3

    Copy
    82
    

    Sample Input 4

    Copy
    3
    0 1000000000 1000000000
    1000000000 0 1000000000
    1000000000 1000000000 0
    

    Sample Output 4

    Copy
    3000000000
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<iomanip>
     6 #include<algorithm>
     7 using namespace std;
     8 #define inf 1<<29
     9 int t,n;
    10 long long  dis[305][305];
    11 long long  d[305][305];
    12 void init()
    13 {
    14     for(int i=1;i<=n;i++)
    15         for(int j=1;j<=n;j++)
    16        dis[i][j]=d[i][j];
    17 }
    18 bool panduan()
    19 {
    20       for(int i=1;i<=n;i++)
    21         for(int j=1;j<=n;j++)
    22        if(dis[i][j]!=d[i][j]) return false;
    23        return true;
    24 }
    25 long long  sum()
    26 {
    27     long long  s=0,fond;
    28     for(int i=1;i<=n;i++)
    29         for(int j=i+1;j<=n;j++)
    30         {
    31             fond=1;
    32             for(int k=1;k<=n;k++){
    33                     if(k==i||k==j) continue;
    34             if(dis[i][j]==dis[i][k]+dis[k][j])
    35                  fond=0;
    36             }
    37             if(fond) s+=dis[i][j];
    38         }
    39 
    40     return s;
    41 }
    42 int main()
    43 {
    44         cin>>n;
    45      for(int i=1;i<=n;i++)
    46             for(int j=1;j<=n;j++)
    47             cin>>d[i][j];
    48         init();
    49         for(int k=1;k<=n;k++)
    50              for(int i=1;i<=n;i++)
    51               for(int j=1;j<=n;j++)
    52                 if(d[i][j]>d[i][k]+d[k][j])
    53                  d[i][j]=d[i][k]+d[k][j];
    54         int t=panduan();
    55         if(!t) cout<<"-1"<<endl;
    56       else cout<<sum()<<endl;
    57             return 0;
    58 }


  • 相关阅读:
    Python day thirteen
    Python day twelve
    Python day eleven
    Python day ten
    Python day nine
    Python day eight
    Python day seven
    Python day six
    Python day five
    Python day four
  • 原文地址:https://www.cnblogs.com/blvt/p/7534898.html
Copyright © 2011-2022 走看看