洛谷 - 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就有多个 ...
随机推荐
- 线程优先级队列( Queue)
Python的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue.这些队列都实现 ...
- 南京网络赛C
分段打表大法好!!! 打表40min,A题1s https://nanti.jisuanke.com/t/41300 #include<bits/stdc++.h> #define int ...
- [LeetCode]-DataBase-Nth Highest Salary
Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...
- DjangoRestFrameWork 版本控制
DRF的版本控制 为什么需要版本控制 API 版本控制允许我们在不同的客户端之间更改行为(同一个接口的不同版本会返回不同的数据). DRF提供了许多不同的版本控制方案. 可能会有一些客户端因为某些原因 ...
- springMVC @response 中文乱码解决
以下任选一种 在web.xml中: 添加一个过滤器(filter),注册 org.springframework.web.filter.CharacterEncodingFilter <filt ...
- Android NDK下载
http://dl.google.com/android/ndk/android-ndk-r10d-linux-x86_64.bin https://dl.google.com/android/rep ...
- Linux_KVM虚拟机
目录 目录 Hpyervisor的种类 安装KVM 使用virsh指令管理虚拟机 KVM虚拟机的网络设置 Hpyervisor的种类 hpyervisor:是一种VMM(Virtual Machine ...
- Python Module_os_操作系统
目录 目录 前言 软件环境 os模块内建属性 osname 获取执行平台的类型 oslinesep 输出当前平台使用的行终止符 ossep 输出操作系统特定的路径分隔符 ospathsep 输出用于分 ...
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_3 mybatis连接池的分类
2.mybatis中的连接池 mybatis连接池提供了3种方式的配置: 配置的位置: 主配置文件SqlMapConfig.xml中的dataSourc ...
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_2 Mybatis中编写dao实现类的使用-保存操作
再完善.saveUser的方法 测试保存的操作 报错了 SqlSession的insert的源码 我们在执行Insert的时候,并没有把user对象传过去 usersex改成sex 再次测试