贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。
问题 C: Restoring Road Network
提交: 896 解决: 184
[提交] [状态] [讨论版] [命题人:admin]
[提交][状态]
问题 C: Restoring Road Network
时间限制: 1 Sec 内存限制: 128 MB提交: 896 解决: 184
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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
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
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.
这个题目一开始没读懂,明明是最短路了为啥还要最短路,赛后查题解才知道,原来是需要你进行判断。。。。
自己为什么这么菜。注意 he thought !!!!
判断的时候 如果 经过一个额外的一点的距离小于所给的数据,那么他就不是最短距离。
#include <iostream>
using namespace std;
int a[310][310];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int flag=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(a[i][k]+a[k][j]<a[i][j]&&i!=j&&j!=k&&i!=k){
flag=1;
break;
}
if(a[i][k]+a[k][j]==a[i][j]&&i!=j&&j!=k&&i!=k){
a[i][k]=0;//这里不应该赋值为0,会影响之后的判断。
a[k][i]=0;
}
}
if(flag) {
break;
}
}
if(flag){
break;
}
}
if(flag){
cout<<-1;
}
else{
int sum=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
sum+=a[i][j];
+1;j<n;j++){
if(v[i][j]!=1){`}
sum+=a[i][j];
}
}
}
cout<<sum;
}
}
}
}
cout<<sum;
接下来是修改过的代码
#include <iostream>
using namespace std;
int a[310][310];
int v[310][310];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int flag=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(a[i][k]+a[k][j]<a[i][j]&&i!=j&&j!=k&&i!=k){
flag=1;
cout<<-1;
return 0;
}
if(a[i][k]+a[k][j]==a[i][j]&&i!=j&&j!=k&&i!=k){
v[i][j]=1;
v[j][i]=1;
}
}
}
}
long int sum=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){//只需要遍历上三角或者下三角就可以了
if(v[i][j]!=1){//在相同路径下尽可能的走经过点数多的路线
sum+=a[i][j];
}
}
}
cout<<sum;
}