当前位置:网站首页>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;
}
边栏推荐
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- Preprocessing and preprocessing macros
- Date tool class (updated from time to time)
- Reading notes of "the way to clean structure" (Part 2)
- Usage of ieda refactor
- 安装单机redis详细教程
- 电脑使用哪个录制视频软件比较好
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- Data dimensionality reduction principal component analysis
猜你喜欢

《架构整洁之道》读书笔记(下)

全志A33使用主线U-Boot

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

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

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

Markdown基础语法
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

ICDE 2023|TKDE Poster Session(CFP)

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept
随机推荐
程序猿入门攻略(十二)——数据的存储
AcWing 1134. 最短路计数 题解(最短路)
In pytorch function__ call__ And forward functions
【pytorch学习笔记】Tensor
Juypter notebook modify the default open folder and default browser
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
SIFT feature point extraction "suggestions collection"
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
思考变量引起的巨大变化
MySQL高级(进阶)SQL语句
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
冒泡排序数组
Use cheat engine to modify money, life and stars in Kingdom rush
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Which video recording software is better for the computer
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
GMapping代码解析[通俗易懂]
使用xml文件打印mybaties-log插件的方式
[pytorch learning notes] tensor