USACO 2.4 Cow Tours
Cow Tours
Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pastures that cannot be connected by any sequence of cow paths, thus partitioning Farmer John's farm into multiple fields.
Farmer John would like add a single a cow path between one pair of pastures using the constraints below.
A field's `diameter' is defined to be the largest distance of all the shortest walks between any pair of pastures in the field. Consider the field below with five pastures, located at the points shown, and cow paths marked by lines:
15,15 20,15
D E
*-------*
| _/|
| _/ |
| _/ |
|/ |
*--------*-------*
A B C
10,10 15,10 20,10
The `diameter' of this field is approximately 12.07106, since the longest of the set of shortest paths between pairs of pastures is the path from A to E (which includes the point set {A,B,E}). No other pair of pastures in this field is farther apart when connected by an optimal sequence of cow paths.
Suppose another field on the same plane is connected by cow paths as follows:
*F 30,15
/
_/
_/
/
*------
G H
25,10 30,10
In the scenario of just two fields on his farm, Farmer John would add a cow path between a point in each of these two fields (namely point sets {A,B,C,D,E} and {F,G,H}) so that the joined set of pastures {A,B,C,D,E,F,G,H} has the smallest possible diameter.
Note that cow paths do not connect just because they cross each other; they only connect at listed points.
The input contains the pastures, their locations, and a symmetric "adjacency" matrix that tells whether pastures are connected by cow paths. Pastures are not considered to be connected to themselves. Here's one annotated adjacency list for the pasture {A,B,C,D,E,F,G,H} as shown above:
A B C D E F G H
A 0 1 0 0 0 0 0 0
B 1 0 1 1 1 0 0 0
C 0 1 0 0 1 0 0 0
D 0 1 0 0 1 0 0 0
E 0 1 1 1 0 0 0 0
F 0 0 0 0 0 0 1 0
G 0 0 0 0 0 1 0 1
H 0 0 0 0 0 0 1 0
Other equivalent adjacency lists might permute the rows and columns by using some order other than alphabetical to show the point connections. The input data contains no names for the points.
The input will contain at least two pastures that are not connected by any sequence of cow paths.
Find a way to connect exactly two pastures in the input with a cow path so that the new combined field has the smallest possible diameter of any possible pair of connected pastures. Output that smallest possible diameter.
PROGRAM NAME: cowtour
INPUT FORMAT
Line 1: | An integer, N (1 <= N <= 150), the number of pastures |
Line 2-N+1: | Two integers, X and Y (0 <= X ,Y<= 100000), that denote that X,Y grid location of the pastures; all input pastures are unique. |
Line N+2-2*N+1: | lines, each containing N digits (0 or 1) that represent the adjacency matrix as described above, where the rows' and columns' indices are in order of the points just listed. |
SAMPLE INPUT (file cowtour.in)
8
10 10
15 10
20 10
15 15
20 15
30 15
25 10
30 10
01000000
10111000
01001000
01001000
01110000
00000010
00000101
00000010
OUTPUT FORMAT
The output consists of a single line with the diameter of the newly joined pastures. Print the answer to exactly six decimal places. Do not perform any special rounding on your output.
SAMPLE OUTPUT (file cowtour.out)
22.071068 ————————————————————————题解
应该是简单的最短路……以及检查连通性的并查集
然后就不难了……毕竟只是加一条路而已
要注意的是:接完一条路之后的两个联通块的最长路径最小,这个值可能会比不上某个单独的联通块的最长路径。
嗯。
/*
ID: ivorysi
PROG: cowtour
LANG: C++
*/ #include <iostream>
#include <string.h>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <ctime>
#include <cmath>
#include <queue>
#define ivorysi
#define mo 1000000007
#define siji(i,x,y) for(int i=(x);i<=(y);i++)
#define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
#define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
#define ivory(i,x) for(int i=head[x];i;i=edge[i].n)
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x5f5f5f5f
#define N 5005
typedef long long ll;
using namespace std;
pii poi[];
char adj[][];
double leng[][];
int id1[],id2[],cnt1,cnt2;
int n,fa[];
int op[];
double ans=;
int powt(int a) {return a*a;}
int getfa(int x) {return fa[x]==x?x:fa[x]=getfa(fa[x]);}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("cowtour.in","r",stdin);
freopen("cowtour.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
scanf("%d",&n);
siji(i,,n) {
scanf("%d %d",&poi[i].fi,&poi[i].se);
}
getchar();
siji(i,,n) fa[i]=i;
siji(i,,n) {
siji(j,,n) {
leng[i][j]=;
}
}
siji(i,,n) leng[i][i]=;
siji(i,,n) {
scanf("%s",adj[i]+);
siji(j,,n) {
if(adj[i][j]=='') {
fa[getfa(i)]=getfa(j);
leng[i][j]=sqrt((double)powt(poi[i].fi-poi[j].fi)+powt(poi[i].se-poi[j].se));
}
}
}
siji(k,,n) {
siji(i,,n) {
siji(j,,n) {
leng[i][j]=min(leng[i][k]+leng[k][j],leng[i][j]);
}
}
}
siji(i,,n) op[i]=i;
siji(i,,n) {
siji(j,,n) {
if(i!=j && leng[i][j]< ) {
if(leng[i][op[i]]<leng[i][j]) op[i]=j;
}
}
}
siji(i,,n) {
siji(j,,n) {
if(getfa(i)!=getfa(j)) {
double tmp=sqrt((double)powt(poi[i].fi-poi[j].fi)+powt(poi[i].se-poi[j].se));
tmp=tmp+leng[i][op[i]]+leng[j][op[j]];
ans=min(ans,tmp);
}
}
}
siji(i,,n) {
ans=max(ans,leng[i][op[i]]);//单独的联通块里的最大值过一遍
}
printf("%.6lf\n",ans);
}
USACO 2.4 Cow Tours的更多相关文章
- 洛谷P1522 牛的旅行 Cow Tours
---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...
- 洛谷 P1522 牛的旅行 Cow Tours 题解
P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...
- 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours
洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...
- [图论]牛的旅行 Cow Tours :Floyed-Warshall
牛的旅行 Cow Tours 目录 牛的旅行 Cow Tours 题目描述 输入格式 输出格式 输入输出样例 输入 #1 输出 #1 解析 代码 题目描述 农民 John的农场里有很多牧区.有的路径连 ...
- 【USACO 2.4】Cow Tours (最短路)
题意:给你n(最多150)个点的坐标,给出邻接矩阵,并且整个图至少两个联通块,现在让你连接一条边,使得所有可联通的两点的最短距离的最大值最小. 题解:先dfs染色,再用floyd跑出原图的直径O($n ...
- USACO 2.3 Cow Pedigrees
Cow Pedigrees Silviu Ganceanu -- 2003 Farmer John is considering purchasing a new herd of cows. In t ...
- USACO 6.1 Cow XOR
Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...
- USACO 2012 Feb Cow Coupons
2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...
- Luogu P1522 牛的旅行 Cow Tours
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
随机推荐
- Unity Get Thread Content Failed
最近在使用Unity做项目时,发现总是莫名的出现“Get Thread Content Failed”的消息弹出,然后Unity就卡死了,这样反反复复,后来查到是因为一些杀毒软件在阻止Unity,尝试 ...
- Docker环境下如何安装Zookeeper
第一步:首先下载Zookeeper的镜像文件: 从仓库中pull 这个zookeeper镜像:docker pull jplock/zookeeper:3.4.8 然后docker images查看该 ...
- Discuz! X3.2重置管理员账号
主要是使用了Tools急诊箱.先看一下Tools急诊箱的主要功能: 多种模式在线安装Discuz!,或者重装 重置管理员账号:将把您指定的会员设置为管理员 关闭功能:一键关闭/打开 [站点|插件]的操 ...
- hdu 1907 John&& hdu 2509 Be the Winner(基础nim博弈)
Problem Description Little John is playing very funny game with his younger brother. There is one bi ...
- .net c#通过Exif获取图片信息(参数)
简介 想要获取图片的信息,例如快门速度.ISO值等等,我们可以通过读取Exif中存储的信息.Exif(Exchangeable Image File)是存储在JPEG格式照片头部的一段信息,相机和手机 ...
- 2016 ACM/ICPC Asia Regional Qingdao Online
吐槽: 群O的不是很舒服 不知道自己应该干嘛 怎样才能在团队中充分发挥自己价值 一点都不想写题 理想中的情况是想题丢给别人写 但明显滞后 一道题拖沓很久 中途出岔子又返回来搞 最放心的是微软微软妹可以 ...
- Rosenbrock function
w测试最优化算法性能可通过其. https://en.wikipedia.org/wiki/Rosenbrock_function https://zh.wikipedia.org/wiki/Rose ...
- PAT乙级1006. 换个格式输出整数 (15)
让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个“百”. ...
- 如何在MyEclipse中配置jre的编译运行环境
由于在MyEclipse中已经自带了jre编译环境,但由于版本太低,所以有时候需要将编译环境配置为系统的jre版本.在MyEclipse中配置jre的编译运行环境很简单,只需要全局配置一次,则所有项目 ...
- MPMoviePlayerController
属性 说明 @property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径 @property (nonatom ...