Original title address: Http://abc074.contest.atcoder.jp/tasks/arc083_b
Time Limit:2sec/ Memory LIMIT:256MB
Score: 500 points
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 i≠j , 1≤Ai,j=Aj,i≤109 .
- Ai,i=0
Inputs
Input is given from standard Input in the following format:
NA1,1A1,2…A1,NA2,1A2,2…A2,N…AN,1AN,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
The title means: There is a map above marks the size of the shortest path between each point; To determine the total distance, if the shortest circuit and the map does not meet the output-1;
Problem-Solving ideas: Floyd algorithm, if the two points between the relay on the record;
Code:
#include <iostream>#include<string>#include<algorithm>#include<string.h>#include<stdio.h>#include<math.h>#include<Set>#include<queue>#include<stack>#include<map>using namespaceStd;typedefLong LongLL;Const intMAX = the; intvisit[max][max]={0}; LL Map1[max][max]; LL Map[max][max];intN;intMain () {CIN>>N; for(inti =1; i<=n;i++) for(intj =1; j<=n;j++) {cin>>Map[i][j]; MAP1[I][J]=Map[i][j]; } for(intK =1; k<=n;k++) for(inti =1; i<=n;i++) for(intj =1; j<=n;j++){ if(map[i][j]>=map[i][k]+map[k][j]&& (i!=k&&j!=k)) { //cout<<i<< "" <<j<< "" <<k<<endl;MAP1[I][J] = map[i][k]+Map[k][j]; VISIT[I][J]=1; }} LL sum=0; for(inti =1; i<n;i++){ for(intj = i+1; j<=n;j++) { if(map[i][j]!=Map1[i][j]) {cout<<"-1"<<Endl; return 0; } if(map[i][j]==map1[i][j]&&visit[i][j]!=1) Sum+=Map[i][j]; } } if(sum==0) cout<<-1<<Endl; Elsecout<<sum<<Endl; return 0;}
D-restoring Road Network