当前位置:网站首页>AcWing 1125. Cattle travel problem solution (shortest path, diameter)
AcWing 1125. Cattle travel problem solution (shortest path, diameter)
2022-07-02 19:32:00 【Mr. Qiao Da】
AcWing 1125. The journey of cattle
What is more troublesome is the calculation of the diameter and the diameter after connecting the points of two different pastures ,floyd The necessity of seeking the shortest path is not very strong
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define db double
typedef pair<db, db>PDD;
const int N = 200;
const double MNF = 1e20;
int n, m;
char g[N][N]; // Storage matrix
PDD q[N]; // Store coordinates
db d[N][N]; // Store the shortest distance
db maxd[N]; // The longest distance to store in the same pasture
db get_dist(PDD a, PDD b){
// Find the distance between two points
db dx = a.x - b.x, dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
int main()
{
cin>>n;
for(int i = 0; i < n; i ++ ){
cin>>q[i].x>>q[i].y;
}
for(int i = 0; i < n; i ++ ) cin>>g[i];
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < n; j ++ ){
if(i == j) d[i][j] = 0;
else if(g[i][j] == '1') d[i][j] = get_dist(q[i], q[j]);
else d[i][j] = MNF;
}
}
//Floyd Find the shortest path
for(int k = 0; k < n; k ++ ){
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < n; j ++ ){
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
db res1 = 0;
for(int i = 0; i < n ; i ++ ){
for(int j = 0; j < n; j ++ ){
if(d[i][j] < MNF / 2){
// Here I'm wondering why I can't use the matrix 1 Judge two points that are not connected
// The reason is that the number of matrices of two indirectly connected points in the same pasture is also 1, Therefore, the value of the matrix cannot be used to judge whether two points are a pasture
maxd[i] = max(d[i][j], maxd[i]); // Find the diameter of each point
}
}
res1 = max(maxd[i], res1);
}
db res = MNF; // initialization
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < n; j ++ ){
if(d[i][j] > MNF / 2){
// Here I wonder why I can't use the 0 Judge two points that are not connected
// The reason is that the matrix number of two points that are not directly connected in the same pasture is also 0, Therefore, it is impossible to judge whether two points are a pasture with a matrix
res = min(res, maxd[i] + maxd[j] + get_dist(q[i], q[j]));
}
}
}
printf("%.6lf\n", max(res, res1));
return 0;
}
边栏推荐
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- 数据降维——主成分分析
- Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
- In pytorch function__ call__ And forward functions
- 预处理和预处理宏
- What is the MySQL backup suffix_ MySQL backup restore
- "Patient's family, please come here" reading notes
- 2022.7.1-----leetcode.241
- Introduction of Ethernet PHY layer chip lan8720a
- AcWing 1129. 热浪 题解(最短路—spfa)
猜你喜欢

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode

Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others

Data dimensionality reduction principal component analysis

Yolov3 trains its own data set to generate train txt

Develop fixed asset management system, what voice is used to develop fixed asset management system

Quanzhi A33 uses mainline u-boot

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

Yunna | why use the fixed asset management system and how to enable it

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
随机推荐
Golang concurrent programming goroutine, channel, sync
安装单机redis详细教程
冒泡排序数组
PHP非对称加密方法私钥及公钥加密解密的方法
AcWing 1135. 新年好 题解(最短路+搜索)
思考变量引起的巨大变化
《架构整洁之道》读书笔记(下)
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
In pytorch function__ call__ And forward functions
电脑使用哪个录制视频软件比较好
QT中的QPropertyAnimation使用和toast案列
C文件输入操作
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
移动机器人路径规划:人工势场法[通俗易懂]
Usage of ieda refactor
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
预处理和预处理宏
AcWing 1137. 选择最佳线路 题解(最短路)
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
MySQL高级(进阶)SQL语句