洛谷 - 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就有多个 ...
随机推荐
- windows 环境如何启动 redis ?
1.cd 到 redis 的安装目录 C:\Users\dell>cd C:\redis 2.执行 redis 启动命令 C:\redis>redis-server.exe redis.w ...
- java文件分片上传,断点续传
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- java大文件上传
上次遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误.查看了系统的配置文件没有看到文件大小限制,web.xml中sees ...
- MongoDB基本操作(增删改查)
基本操作 基本的“增删查改“,在DOS环境下输入mongo命令打开shell,其实这个shell就是mongodb的客户端,同时也是一个js的编译器,默认连接的是“test”数据库.
- 【POJ2992】Divisors
[题目概括] 计算\(C_n^k\)的因子个数. [思路要点] 首先考虑将组合数展开,展开后就是\(\frac {n!}{k!\times (n-k)!}\). 这样就是计算出这些质因子的个数,然后将 ...
- ModelSerializer 使用知识点_serializer.save(project=obj) #外键一定要作为实例传入save函数,否则无法新增成功
1.有两个模型如下 A.project class Project(models.Model): """ 项目表 """ id = mode ...
- Redis配置文件全解
基本配置 port 6379 # 监听端口号,默认为 6379,如果你设为 0 ,redis 将不在 socket 上监听任何客户端连接. daemonize no #是否以后台进程启动 datab ...
- Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
if first time to install docker, be noted the docker engine started as root copied from: http://blog ...
- 对AC自动机+DP题的一些汇总与一丝总结 (1)
(1)题意 : 输入n.m.k意思就是给你 m 个模式串,问你构建长度为 n 至少包含 k 个模式串的方案有多少种 分析:(HDU2825) DP[i][j][k] 表示 DP[第几步][哪个节点结尾 ...
- 运维自动化之ansible的安装与使用 转
运维自动化之ansible的安装与使用 随着服务器数量的增长,我们需要一个批量工具去提高工作效率,之前用的是puppet,ansible的简单,适用让我眼前一亮,决定写一篇ansible从安装到基本配 ...