当前位置:网站首页>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;
}
边栏推荐
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- PHP非对称加密方法私钥及公钥加密解密的方法
- C file input operation
- Refactoring: improving the design of existing code (Part 1)
- Microservice technology - distributed global ID in high concurrency
- Reading notes of "the way to clean structure" (Part 2)
- AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
- PyTorch函数中的__call__和forward函数
- 程序猿入门攻略(十二)——数据的存储
猜你喜欢
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Data dimensionality reduction factor analysis
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
Refactoring: improving the design of existing code (Part 1)
Reading notes of "the way to clean structure" (Part 2)
Juypter notebook modify the default open folder and default browser
随机推荐
Virtual machine initialization script, virtual machine mutual secret key free
搭建哨兵模式reids、redis从节点脱离哨兵集群
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
Advanced performance test series "24. Execute SQL script through JDBC"
Golang concurrent programming goroutine, channel, sync
Emmet basic syntax
Memory management of C
虚拟机初始化脚本, 虚拟机相互免秘钥
A4988 drive stepper motor "recommended collection"
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
AcWing 383. 观光 题解(最短路)
Markdown basic grammar
预处理和预处理宏
Gmapping code analysis [easy to understand]
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Use cheat engine to modify money, life and stars in Kingdom rush
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Yolov3 trains its own data set to generate train txt
Compile oglpg-9th-edition source code with clion