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. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  2. pycharm如何添加固定代码块

    1. file -- settings -- 搜索框输入live,找到 Live Templates 2. 选择你要添加到哪个语言中去,打开python组,并点击右上角 “+”,选择 1.Live T ...

  3. jenkins 管理员密码重置

    jenkins管理员 admin的密码忘记怎么重置呢? 修改admin的加密密码为123456的加密密码 #jbcrypt:$2a$10$MiIVR0rr/UhQBqT.bBq0QehTiQVqgNp ...

  4. Spring Cloud云架构 - commonservice-sso服务搭建(一)

    前面几篇我们已经介绍了Spring Cloud和oauth2的知识点,今天我们要利用Spring Cloud和oauth2进行commonservice-sso服务搭建,本节我们只是搭建commons ...

  5. C# 异步编程,async与await的简单学习

    前提声明:C# 5.0 .NET Framework 4.5 2012-08-15 异步和等待(async和await).调用方信息(Caller Information)  (C#版本与.NET版本 ...

  6. wannafly 挑战赛9 E 组一组 (差分约束)

    链接:https://www.nowcoder.com/acm/contest/71/E 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 65536K,其他语言131072K Spec ...

  7. session与cookie区别与联系

    一.Session的概念 Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标 ...

  8. 一致性hash算法Consistent Hashing

    一致性hash算法Consistent Hashing 对于原有hash算法hash%n so... 1.话不多说直接上代码,原理或详解自行百度即可 import cn.pheker.utils.Ut ...

  9. Using FileUpload

    Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...

  10. Quartz安装包中的15个example

    Welcome======= Welcome to the Quartz Examples directory. This directory contains 15 examples that sh ...