当前位置:网站首页>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;
}
边栏推荐
- Data dimensionality reduction principal component analysis
- The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
- Golang concurrent programming goroutine, channel, sync
- 【ERP软件】ERP体系二次开发有哪些危险?
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- [test development] takes you to know what software testing is
- High frequency interview questions
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
猜你喜欢

Quanzhi A33 uses mainline u-boot

IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config

Refactoring: improving the design of existing code (Part 1)

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

新手必看,点击两个按钮切换至不同的内容

Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config

450-深信服面经1

zabbix5客户端安装和配置
随机推荐
Date tool class (updated from time to time)
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
2022 software engineering final exam recall Edition
Markdown基础语法
golang:[]byte转string
PHP asymmetric encryption method private key and public key encryption and decryption method
Binary operation
Bubble sort array
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
2022 compilation principle final examination recall Edition
股票证券公司排名,有安全保障吗
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Machine learning notes - time series prediction research: monthly sales of French champagne
Quanzhi A33 uses mainline u-boot
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Notes de lecture sur le code propre
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Usage of ieda refactor
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
Emmet基础语法