当前位置:网站首页>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;
}
边栏推荐
- codeforces每日5题(均1700)-第四天
- AcWing 1131. 拯救大兵瑞恩 题解(最短路)
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- Yunna | why use the fixed asset management system and how to enable it
- Microservice technology - distributed global ID in high concurrency
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- Preprocessing and preprocessing macros
- Yolov3 trains its own data set to generate train txt
- IEDA refactor的用法
猜你喜欢
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Reading notes of "the way to clean structure" (Part 2)
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
Use cheat engine to modify money, life and stars in Kingdom rush
Quanzhi A33 uses mainline u-boot
Yolov3 trains its own data set to generate train txt
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
ICDE 2023|TKDE Poster Session(CFP)
Yunna | why use the fixed asset management system and how to enable it
随机推荐
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Memory management of C
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
二进制操作
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
搭建哨兵模式reids、redis从节点脱离哨兵集群
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
AcWing 1134. 最短路计数 题解(最短路)
Golang concurrent programming goroutine, channel, sync
数据降维——因子分析
453-atoi函数的实现
Golang:[]byte to string
使用xml文件打印mybaties-log插件的方式
Watchful pioneer world outlook Architecture - (how does a good game come from)
安装单机redis详细教程
2022 software engineering final exam recall Edition
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
AcWing 1135. 新年好 题解(最短路+搜索)
juypter notebook 修改默认打开文件夹以及默认浏览器