标签:continue int res void 一个 which distance oid nts
个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况,
后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧。
题意概述:
就是给定一个N阶方阵,规定Auv,为u到v的最短路径,若给出的数据存在其他通路少于此时的值则不存在即为假,
解决方法就是利用Floyd算法进行单源最短路的判断,只要后面的矩阵与原来的不相符就是假的。真的的时候,是要求
存在的最短总路程使得矩阵的数成立,我画了下就是只要存在从其他城市能够转到目的地的时候就可以不要这条直接到达的
路,当时脑袋短路没有弄出来,后面一想只要对于每条路进行判断,若存在这样的路就不加在sum里面就好了
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:
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.
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:
3 0 1 3 1 0 1 3 1 0
-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.
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
82
3 0 1000000000 1000000000 1000000000 0 1000000000 1000000000 1000000000 0
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 }
Restoring Road Network(Floyd算法的推广)
标签:continue int res void 一个 which distance oid nts
原文地址:http://www.cnblogs.com/blvt/p/7534898.html