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的更多相关文章

  1. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  2. 洛谷 P1522 牛的旅行 Cow Tours 题解

    P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...

  3. 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours

    洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...

  4. [图论]牛的旅行 Cow Tours :Floyed-Warshall

    牛的旅行 Cow Tours 目录 牛的旅行 Cow Tours 题目描述 输入格式 输出格式 输入输出样例 输入 #1 输出 #1 解析 代码 题目描述 农民 John的农场里有很多牧区.有的路径连 ...

  5. 【USACO 2.4】Cow Tours (最短路)

    题意:给你n(最多150)个点的坐标,给出邻接矩阵,并且整个图至少两个联通块,现在让你连接一条边,使得所有可联通的两点的最短距离的最大值最小. 题解:先dfs染色,再用floyd跑出原图的直径O($n ...

  6. USACO 2.3 Cow Pedigrees

    Cow Pedigrees Silviu Ganceanu -- 2003 Farmer John is considering purchasing a new herd of cows. In t ...

  7. USACO 6.1 Cow XOR

    Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...

  8. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

  9. Luogu P1522 牛的旅行 Cow Tours

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

随机推荐

  1. C语言之原码、反码和补码

    原码.反码和补码 1).数据在内存中存储的时候都是以二进制的形式存储的. int num = 10; 原码.反码.补码都是二进制.只不过是二进制的不同的表现形式. 数据是以补码的二进制存储的. 2). ...

  2. springmvc+mybatis+spring 整合 SSM

    A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技 ...

  3. springmvc中返回页面,只在iframe中跳转,而不是整个页面,解决方法。

    问题描述:在我写的主页面中用到了iframe,其中在iframe中有一个button点击提交,然后在Controller中返回页面的时候,是在iframe中打开的,而不是整个页面. 最开始我想用aja ...

  4. 【USACO】滑雪课程

    滑雪课程贝西去科罗拉多州去滑雪,不过还她不太会玩,只是个能力为 1 的渣渣.贝西从 0 时刻进入滑雪场,一到 T 时刻就必须离开.滑雪场里有 N 条斜坡,第 i 条斜坡滑行一次需要 Di 分钟,要求游 ...

  5. c#dalegate invoke及AsyncCallback使用

    public delegate void AsyncDelegate();        private AsyncDelegate asyncDl; asyncDl = new AsyncDeleg ...

  6. Codeforces Round #390 (Div. 2)

    时隔一个月重返coding…… 期末复习了一个月也不亏 倒是都过了…… 就是计组61有点亏 复变68也太低了 其他都还好…… 假期做的第一场cf 三道题 还可以…… 最后room第三 standing ...

  7. Chapter 21_5.2 tab扩展

    在Lua中,像这样()的空白捕获具有特殊意义.表示捕获它在目标字符串中的位置,返回一个数字: print(string.match("hello","()ll()&quo ...

  8. 【安装】beautifulsoup4—美丽汤的安装

    beautifulsoup俗称美丽汤,是用来爬虫用的,大家可以到这个网址去下载.注意,要根据对应的python版本 来下载. 下载传送: https://pypi.python.org/pypi/be ...

  9. POJ 3139 Balancing the Scale

    枚举. 看了这个方法:$http://www.cppblog.com/shiming413/archive/2008/12/21/29671.html$ 将数字归类的地方不能用$vector$,会超时 ...

  10. JavaScript基础(更新第二波)

    下面接着说JavaScript打开新的窗口. open()方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL]),[窗口名称],[参数字符串] 参数说明: URL ...