当前位置:网站首页>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;
}
边栏推荐
- Notes de lecture sur le code propre
- Talk about the design of red envelope activities in e-commerce system
- "Patient's family, please come here" reading notes
- 二进制操作
- 2022.7.1-----leetcode.241
- 为什么要做企业固定资产管理系统,企业如何加强固定资产管理
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- PHP非对称加密方法私钥及公钥加密解密的方法
- Advanced performance test series "24. Execute SQL script through JDBC"
猜你喜欢
End to end object detection with transformers (Detr) paper reading and understanding
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Yolov3 trains its own data set to generate train txt
全志A33使用主线U-Boot
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
线程应用实例
PHP parser badminton reservation applet development requires online system
PHP-Parser羽毛球预约小程序开发require线上系统
《重构:改善既有代码的设计》读书笔记(上)
In pytorch function__ call__ And forward functions
随机推荐
Use cheat engine to modify money, life and stars in Kingdom rush
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
数据降维——主成分分析
MySQL table historical data cleaning summary
Excel finds the same value in a column, deletes the row or replaces it with a blank value
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
AcWing 383. 观光 题解(最短路)
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
函数高阶-柯里化实现
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Binary operation
What is 9D movie like? (+ common sense of dimension space)
AcWing 1135. 新年好 题解(最短路+搜索)
Mobile robot path planning: artificial potential field method [easy to understand]
SIFT feature point extraction "suggestions collection"
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Codeforces Round #802 (Div. 2) 纯补题
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5