当前位置:网站首页>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;
}
边栏推荐
- PHP parser badminton reservation applet development requires online system
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- Virtual machine initialization script, virtual machine mutual secret key free
- 《架构整洁之道》读书笔记(下)
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- AcWing 383. 观光 题解(最短路)
- Microservice technology - distributed global ID in high concurrency
- C file input operation
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Memory management of C
猜你喜欢

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)

《重构:改善既有代码的设计》读书笔记(上)

ICDE 2023|TKDE Poster Session(CFP)

Talk about the design of red envelope activities in e-commerce system

juypter notebook 修改默认打开文件夹以及默认浏览器

mysql函数

Data dimensionality reduction factor analysis
Bubble sort array
随机推荐
codeforces每日5题(均1700)-第四天
数据降维——因子分析
"Patient's family, please come here" reading notes
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
以太网PHY层芯片LAN8720A简介
Markdown基础语法
高级性能测试系列《24. 通过jdbc执行sql脚本》
SIFT特征点提取「建议收藏」
What is the MySQL backup suffix_ MySQL backup restore
Getting started with typescript
Data dimensionality reduction principal component analysis
PHP非对称加密方法私钥及公钥加密解密的方法
《重构:改善既有代码的设计》读书笔记(上)
Golang concurrent programming goroutine, channel, sync
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
Reading notes of "the way to clean structure" (Part 2)
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
PHP asymmetric encryption method private key and public key encryption and decryption method