洛谷 - P1522 - 牛的旅行 - Cow Tours - Floyd
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的更多相关文章
- 洛谷P1522 牛的旅行 Cow Tours
---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...
- 洛谷 P1522 牛的旅行 Cow Tours 题解
P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...
- 洛谷 P1522 牛的旅行 Cow Tours
题目链接:https://www.luogu.org/problem/P1522 思路:编号,然后跑floyd,这是很清楚的.然后记录每个点在这个联通块中的最远距离. 然后分连通块,枚举两个点(不属于 ...
- 洛谷 P1522 牛的旅行 Cow Tours——暴力枚举+最短路
先上一波题目 https://www.luogu.org/problem/P1522 这道题其实就是给你几个相互独立的连通图 问找一条新的路把其中的两个连通图连接起来后使得新的图中距离最远的两个点之 ...
- 洛谷P1522牛的旅行——floyd
题目:https://www.luogu.org/problemnew/show/P1522 懒于仔细分情况而直接像题解那样写floyd然后不明白最后一步max的含义了... 分开考虑怎么保证在一个内 ...
- Luogu P1522 牛的旅行 Cow Tours
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- P1522 牛的旅行 Cow Tours floyed
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- P1522 牛的旅行 Cow Tours
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- 洛谷P1522 牛的旅行
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
随机推荐
- BZOJ 2217: [Poi2011]Lollipop 构造 + 思维
Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T").现在有m个询问,每个询问是询问有没有一个连 ...
- 删除操作——str.subString(0,str.length()-1)
subString是String的一个方法,格式为: public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个 ...
- noi 求分数序列和 x
求分数序列和 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个分数序列 q1/p1,q2/p2,q3/p3,q4/p4,q5/p5,.... ,其中qi+1= qi+ pi, ...
- Luogu P5469 [NOI2019]机器人 (DP、多项式)
不用FFT的多项式(大雾) 题目链接: https://www.luogu.org/problemnew/show/P5469 (这题在洛谷都成绿题了海星) 题解: 首先我们考虑,一个序列位置最右边的 ...
- BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2716 怎么KD树跑得都那么快啊..我写的CDQ分治被暴虐 做四遍CDQ分治,每次求一个 ...
- Spring Boot教程(二)关于RabbitMQ服务器整合
准备工作 15min IDEA maven 3.0 在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如 ...
- 一款基于jQuery Ajax的等待效果
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- XDebug安装配置教程
笔者的开发环境如下:Windows8.1+Apache+PhpStorm+XDebug+Firefox(XDebug helper 1.4.3插件). 转载http://www.jb51.net/ar ...
- java Map类
实现类 类型区别 HashMap 最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度.HashMap最多只允许一条记录的键为Null(多条会覆盖);允 ...
- p3863 序列
分析 按照时间为下标分块 块内按照大小排序 每次整块整体修改半块暴力重构即可 代码 #include<bits/stdc++.h> using namespace std; #define ...