AtCoder Beginner Contest 074 D - Restoring Road Network(Floyd变形)

题目链接:点我点我

题意:给出一个最短路的图,询问是否正确,如果正确的话,输出遍历所有点的最短路径和。

题解:判断是否正确的,直接再一次Floyd,判断是否会有A[i][k]+A[k][j]<A[i][j](通过中间点k使得两点间距离更短),如果有的话,直接输出-1。

要遍历到所有道路,并且路径和最小,我们可以尽可能用用中间点的路径(A[i][k]+A[k][j]==A[i][j]),这样本来遍历两个点,就可以遍历3个点了,

最后加的时候记得不要从重复加,从最小点往后加,不要再往前加,不然就重复了。

(其实如果重复计算也不要紧,就相当于过去又回来,会是答案的两倍,除以2就行了)

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int N=333;
 6 typedef long long LL;
 7 int A[N][N],idx[N][N];
 8
 9 int main(){
10     int n,flag=1;
11     LL ans=0;
12     cin>>n;
13     for(int i=1;i<=n;i++)
14     for(int j=1;j<=n;j++)
15     cin>>A[i][j],idx[i][j]=0;
16
17     for(int k=1;k<=n;k++){
18         for(int i=1;i<=n;i++){
19             for(int j=1;j<=n;j++){
20                 if(i==j||i==k||j==k) continue;
21                 if(A[i][k]+A[k][j]<A[i][j]) flag=0;
22                 if(A[i][k]+A[k][j]==A[i][j]) idx[i][j]=1;
23             }
24         }
25     }
26
27     if(!flag) {cout<<-1<<endl;return 0;}
28     for(int i=1;i<=n;i++){
29         for(int j=i+1;j<=n;j++){//这边可以直接从1开始,这样的话相当于重复计算一次,最后ans/2就行了
30             if(!idx[i][j]) ans+=A[i][j];
31         }
32     }
33
34     cout<<ans<<endl;
35     return 0;
36 }
时间: 2024-08-24 02:13:42

AtCoder Beginner Contest 074 D - Restoring Road Network(Floyd变形)的相关文章

AtCoder Regular Contest 083 D:Restoring Road Network

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

Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)

Problem Statement There is a cave consisting of NN rooms and MM one-directional passages. The rooms are numbered 11 through NN . Takahashi is now in Room 11 , and Room NN has the exit. The ii -th passage connects Room sisi and Room titi (sisi < titi

Restoring Road Network

D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points 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

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &