当前位置:网站首页>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;
}
边栏推荐
- Binary operation
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
- Preprocessing and preprocessing macros
- Usage of ieda refactor
- C文件输入操作
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 线程应用实例
- A4988 drive stepper motor "recommended collection"
猜你喜欢

数据降维——主成分分析

Talk about the design of red envelope activities in e-commerce system

潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Yolov3 trains its own data set to generate train txt

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

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5

Processing strategy of message queue message loss and repeated message sending

Compile oglpg-9th-edition source code with clion

线程应用实例
随机推荐
Refactoring: improving the design of existing code (Part 1)
PHP-Parser羽毛球预约小程序开发require线上系统
以太网PHY层芯片LAN8720A简介
多态的理解以及作用
Machine learning notes - time series prediction research: monthly sales of French champagne
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
Digital scroll strip animation
《代碼整潔之道》讀書筆記
MySQL
Golang concurrent programming goroutine, channel, sync
Data dimensionality reduction principal component analysis
453-atoi函数的实现
Binary operation
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
metric_logger小解
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3