https://www.luogu.org/problem/P1522

好坑啊,居然还有直径不通过新边的数据,还好不是很多。

注意一定要等Floyd跑完之后再去找连通块的直径,不然一定是INF。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int pre[155]; void init(int n) {
for(int i = 0; i < n; ++i)
pre[i] = i;
} double D[155]; int find(int x) {
return pre[x] == x ? x : pre[x] = find(pre[x]);
}
void unit(int x, int y) {
int fx = find(x), fy = find(y);
if(!(fx == fy))
pre[fx] = fy;
}
bool iscc(int x, int y) {
return find(x) == find(y);
}
double pos[155][2];
double dis_t[155][155];
double dist(int i, int j) {
return sqrt((pos[i][0] - pos[j][0]) * (pos[i][0] - pos[j][0]) + (pos[i][1] - pos[j][1]) * (pos[i][1] - pos[j][1]));
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
init(150);
int n;
cin >> n;
for(int i = 0; i < n; ++i)
cin >> pos[i][0] >> pos[i][1];
char ch;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
dis_t[i][j] = 1e9;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j) {
cin >> ch;
if(ch == '1') {
unit(i, j);
dis_t[j][i] = dis_t[i][j] = dist(i, j);
}
if(i == j)
dis_t[i][j] = 0;
}
for(int k = 0; k < n; ++k)
for(int j = 0; j < n; ++j)
for(int i = 0; i < n; ++i) {
if(iscc(i, j)) {
dis_t[i][j] = min(dis_t[i][j], dis_t[i][k] + dis_t[k][j]);
}
}
double max_dis[155] = {};
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(iscc(i, j)){
max_dis[i] = max(max_dis[i], dis_t[i][j]); }
}
D[find(i)] = max(D[find(i)], max_dis[i]);
}
double minn = 1e9;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(!iscc(i, j)) {
minn = min(minn, max(max(D[find(i)], D[find(j)]), dist(i, j) + max_dis[i] + max_dis[j]));
}
/*char s[20005];
sprintf(s,"%.8f\n", minn);
int pi=0;
for(pi=0;s[pi]!='.';++pi);
s[pi+7]='\0';
puts(s);*/
printf("%.6f\n", minn);
return 0;
}

洛谷 - P1522 - 牛的旅行 - Cow Tours - Floyd的更多相关文章

  1. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  2. 洛谷 P1522 牛的旅行 Cow Tours 题解

    P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...

  3. 洛谷 P1522 牛的旅行 Cow Tours

    题目链接:https://www.luogu.org/problem/P1522 思路:编号,然后跑floyd,这是很清楚的.然后记录每个点在这个联通块中的最远距离. 然后分连通块,枚举两个点(不属于 ...

  4. 洛谷 P1522 牛的旅行 Cow Tours——暴力枚举+最短路

    先上一波题目  https://www.luogu.org/problem/P1522 这道题其实就是给你几个相互独立的连通图 问找一条新的路把其中的两个连通图连接起来后使得新的图中距离最远的两个点之 ...

  5. 洛谷P1522牛的旅行——floyd

    题目:https://www.luogu.org/problemnew/show/P1522 懒于仔细分情况而直接像题解那样写floyd然后不明白最后一步max的含义了... 分开考虑怎么保证在一个内 ...

  6. Luogu P1522 牛的旅行 Cow Tours

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  7. P1522 牛的旅行 Cow Tours floyed

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  8. P1522 牛的旅行 Cow Tours

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  9. 洛谷P1522 牛的旅行

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

随机推荐

  1. linux 内存

    [转]Linux 查看内存(free buffer cache) 转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可 ...

  2. mysql NULL函数 语法

    mysql NULL函数 语法 作用:如果表中的某个列是可选的,那么我们可以在不向该列添加值的情况下插入新记录或更新已有的记录.这意味着该字段将以 NULL 值保存. 说明:NULL 值的处理方式与其 ...

  3. SpringBoot项目中,cookie的设置与销毁

    cookie的设置与销毁 1.设置cookie /** * 设置一个cookie * @param response HttpServletResponse * @param name cookie的 ...

  4. sh_09_打印多条分隔线

    sh_09_打印多条分隔线 def print_line(char, times): """打印单行分隔线 :param char: 分隔字符 :param times: ...

  5. hive里面union all的用法记录

    UNION用于联合多个select语句的结果集,合并为一个独立的结果集,结果集去重. UNION ALL也是用于联合多个select语句的结果集.但是不能消除重复行.现在hive只支持UNION AL ...

  6. ali之monkey学习

    monkey主要用来进行压力测试,稳定性测试 http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980068.html 1,什么是monkey ...

  7. 基于thinkphp开发的项目部署到由宝塔面板创建的LNMP服务器上解决路径出错问题

    一. 环境与版本: 主机:amazon aws EC2主机 系统:Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1039-aws x86_64) 面板:宝塔免费版 6.9. ...

  8. linux命令之查找find &grep

    区别:find找目录下的文件:find+目录路径+条件表达式,grep找文件中的行:grep+匹配正则表达式+文件名 find命令 find命令的一般形式 find命令的常用选项及实例 find与xa ...

  9. 【机器学习速成宝典】模型篇03逻辑斯谛回归【Logistic回归】(Python版)

    目录 一元线性回归.多元线性回归.Logistic回归.广义线性回归.非线性回归的关系 什么是极大似然估计 逻辑斯谛回归(Logistic回归) 多类分类Logistic回归 Python代码(skl ...

  10. @RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

    接下来讲解一下 @RequestMapping  和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法 @RequestMapping 为url映射路 ...