当前位置:网站首页>AcWing 1125. 牛的旅行 题解(最短路、直径)
AcWing 1125. 牛的旅行 题解(最短路、直径)
2022-07-02 18:27:00 【乔大先生】
AcWing 1125. 牛的旅行
比较麻烦的是求直径和连接两个不同牧场的点之后求直径的运算,floyd求最短路在这里体现的必要性不是很强
#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]; //储存矩阵
PDD q[N]; //储存坐标
db d[N][N]; //储存最短距离
db maxd[N]; //储存同一片牧场内的最远距离
db get_dist(PDD a, PDD b){
//求两个点之间的距离
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求最短路
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){
//这里在我在疑惑为什么不能用矩阵中的1判断两个不联通的点
//原因是同一个牧场内两个间接连通的点的矩阵的数也是1,所以不能用矩阵的值判断两个点是否是一个牧场
maxd[i] = max(d[i][j], maxd[i]); //求每个点的直径
}
}
res1 = max(maxd[i], res1);
}
db res = MNF; //初始化
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < n; j ++ ){
if(d[i][j] > MNF / 2){
//这里在想为什么不能用矩阵中的0判断两个不联通的点
//原因是同一个牧场内两个没有直接连通的点的矩阵数也是0,所以不能用矩阵判断两个点是否是一个牧场
res = min(res, maxd[i] + maxd[j] + get_dist(q[i], q[j]));
}
}
}
printf("%.6lf\n", max(res, res1));
return 0;
}
边栏推荐
- Getting started with typescript
- Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
- 线程应用实例
- Introduction to the paper | application of machine learning in database cardinality estimation
- 搭建主从模式集群redis
- Fastdfs installation
- AcWing 383. 观光 题解(最短路)
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- MySQL
- End to end object detection with transformers (Detr) paper reading and understanding
猜你喜欢

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

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

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

High frequency interview questions

450-深信服面经1

Processing strategy of message queue message loss and repeated message sending

ICDE 2023|TKDE Poster Session(CFP)

数据降维——因子分析
随机推荐
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
电脑使用哪个录制视频软件比较好
Thread application instance
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
What is 9D movie like? (+ common sense of dimension space)
全志A33使用主线U-Boot
PyTorch函数中的__call__和forward函数
预处理和预处理宏
Virtual machine initialization script, virtual machine mutual secret key free
Mobile robot path planning: artificial potential field method [easy to understand]
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
数据降维——主成分分析
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
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
Emmet基础语法
Golang concurrent programming goroutine, channel, sync
Fastdfs installation
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5