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. js音乐播放器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat=&quo ...

  2. 抓包分析SIP消息

    I. 实验环境 SIP服务器: Yate服务器, IP:port=10.15.3.34:5060 SIP客户端: 移植Sipdroid的Android网络电话软件, IP: 10.15.3.73, A ...

  3. Linux下的暴力密码在线破解工具Hydra安装及其组件安装-使用

    Linux下的暴力密码在线破解工具Hydra安装及其组件安装-使用 hydra可以破解: http://www.thc.org/thc-hydra,可支持AFP, Cisco AAA, Cisco a ...

  4. JavaScript - 平稳退化

    JavaScript使用window对象的open()方法来创建新的浏览器窗口.这个方法有三个参数:window.open(url,name,features)这三个参数都是可选的.1.第一个参数是想 ...

  5. 在python上获得随机字符

    """今天写一个程序,在想既然可以获得随机数,那我可不可以获得任意字符呢,于是在stackoverflow.com 上找到了方法,几乎都是用导入random,然后再用其它 ...

  6. torisegit 保存帐号密码

    设置 -> git 编辑本地 .git/config 增加 [credential] helper = store

  7. DateTime & UTC 相互转化

    public long ToUnixTime(DateTime date) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.U ...

  8. Tiny6410之重定位代码到SRAM+4096

    重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...

  9. 【IE6的疯狂之九】li在IE中底部空行的BUG

    曾经写过[IE6的疯狂之六]li在IE中底部3像素的BUG(增加浮动解决问题),原文地址:http://www.css88.com/archives/421: IE6 BUG大全: http://ww ...

  10. 第 2 章 Node.js 中的交互式运行环境 —— REPL

    本章内容包括: 如何使用REPL运行环境以及如何在该运行环境中测试各种JavaScript表达式. 如何定义并启动REPL运行环境. Node.js 框架中为REPL运行环境提供了哪些命令以及这些命令 ...