Restoring Road Network (Promotion of Floyd algorithm)

Source: Internet
Author: User

Personal experience: To understand the topic took a lot of time, the back of the real time to achieve a little hasty, just simple to make a judgment whether it is true or false situation,

After looking at the solution to find its real judgment when in fact can solve together, forget, the basis is poor or slow to come.

Test Instructions Overview:

is given an n-order matrix, the specified AUV, the shortest path from u to V, if the given data exist other channels less than the value of this time is not present is false,

The solution is to use the Floyd algorithm for single-source shortest-circuit judgment, as long as the latter matrix and the original is not consistent with the false. Really, it was a request.

The shortest total distance of existence causes the number of matrices to be set up, and I have drawn the following as long as there is no direct arrival from other cities when they can be transferred to their destinations.

Road, when the brain short-circuit did not get out, the back of a think as long as the way to judge, if there is such a road is not added to sum inside the good

Problem Statement

In Takahashi kingdom, which once existed, there is N cities, and some pairs of cities is connected bidirectionally b Y roads. The following is known about the road network:

    • People traveled between cities only through roads. It is possible to reach any city from any other city, via intermediate cities if necessary.
    • Different roads May has had Different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table N N with rows and columns, A in the ruin of Takahashi kingdom. He thought that it represented the shortest distances between the cities along of the roads in the kingdom.

Determine whether there exists a road network such u v that for each and, the integer in the Au,v u -th row an d-th column of is equal to the length of the shortest path from city to City v A u 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:

NA1,1 A1,2 … A1,NA2,1 A2,2 … A2,NAN,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 1Copy
30 1 31 0 23 2 0
Sample Output 1Copy
3

The network below satisfies the condition:

    • City and City are 1 2 connected by a road of length 1 .
    • City and City are 2 3 connected by a road of length 2 .
    • City and City are not 3 1 connected by a road.
Sample Input 2Copy
30 1 31 0 13 1 0
Sample Output 2Copy
-1

As there is a path of length from the city to the city 1 1 2 2 3 and the city to the city, there is a path of length c5/> from the city to the city 1 3 . However, according to the table, the shortest distance between city and city 1 3 must is 3 .

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

Sample Input 3Copy
50 21 18 11 2821 0 13 10 2618 13 0 23 1311 10 23 0 1728 26 13 17 0
Sample Output 3Copy
82
Sample Input 4Copy
30 1000000000 10000000001000000000 0 10000000001000000000 1000000000 0
Sample Output 4Copy
3000000000
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iomanip>6#include <algorithm>7 using namespacestd;8 #defineINF 1<<299 intT,n;Ten Long Longdis[305][305]; One Long Longd[305][305]; A voidInit () - { -      for(intI=1; i<=n;i++) the          for(intj=1; j<=n;j++) -dis[i][j]=D[i][j]; - } - BOOLPanduan () + { -        for(intI=1; i<=n;i++) +          for(intj=1; j<=n;j++) A        if(Dis[i][j]!=d[i][j])return false; at        return true; - } - Long Longsum () - { -     Long Longs=0, fond; -      for(intI=1; i<=n;i++) in          for(intj=i+1; j<=n;j++) -         { toFond=1; +              for(intk=1; k<=n;k++){ -                     if(k==i| | K==J)Continue; the             if(dis[i][j]==dis[i][k]+Dis[k][j]) *Fond=0; $             }Panax Notoginseng             if(Fond) s+=Dis[i][j]; -         } the  +     returns; A } the intMain () + { -Cin>>N; $       for(intI=1; i<=n;i++) $              for(intj=1; j<=n;j++) -Cin>>D[i][j]; - init (); the          for(intk=1; k<=n;k++) -               for(intI=1; i<=n;i++)Wuyi                for(intj=1; j<=n;j++) the                 if(d[i][j]>d[i][k]+D[k][j]) -d[i][j]=d[i][k]+D[k][j]; Wu         intt=Panduan (); -         if(!t) cout<<"-1"<<Endl; About       ElseCout<<sum () <<Endl; $             return 0; -}



Restoring Road Network (Promotion of Floyd algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.