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. Dell-R730 【Pxe+dhcp+ftp+tftp+Kickstart+CentOs6.6】

    IP:10.104.0.101 [root@localhost network-scripts]# cat ifcfg-em1 [root@localhost network-scripts]# ip ...

  2. php目录函数

    1.创建 mkdir()mkdir(目录地址,权限模式,是否递归创建)默认不支持递归创建,用第三个参数true表示递归创建 <?php header("content-type:tex ...

  3. 【CSS学习笔记】初始化CSS后,写li,并利用背景图片,来完成li小图标的效果,且达到个浏览器兼容

    第一种情况 /*当标题前的图标时单独的一个点儿或者方块或者其他类似图标时,定义背景图background要放在<li>里.    在<li>中设置背景图片的尺寸,地址,不重复, ...

  4. LDA进阶(Dynamic Topic Models)

    转自:http://blog.csdn.net/hxxiaopei/article/details/8034308 http://blog.csdn.net/huagong_adu/article/d ...

  5. DW,DM,ODS的区别

    数据仓库的重要应用是将不同来源的数据和异构数据通过ETL整合在一起,为决策分析提供支撑,若在同一个数据库中分不同用户,此意义不大:假设所有有用户都在一个数据库里,如果因为某个原因数据库重启,那么会影响 ...

  6. flexbox备忘

    伸缩项目的父元素: display:flex || display:inline-flex fiex-direction: row(默认) | row-reverse | column | colum ...

  7. 自己找到的一些比较实用比较好看的前端特效。很多是前面提供的jquery网站的

    http://www.cnblogs.com/zhangzongle/p/6034394.html webservicehttp://blog.csdn.net/wowkk/article/detai ...

  8. Ubuntu中Qt+opencv图像显示

    先抛出一个疑问,知道的希望留言解答下 (创建Application->Qt Console Application,代码与下面2.3一样,运行有时会显示图片,但大多数不显示,为什么?) 1.打开 ...

  9. 命令行启动Hololens Emulator,可解决内存不足的问题

    有时候在虚拟机测试时常会出现 内存不足 的情况,导致应用卡顿,调整 /memsize 参数大小: start "HoloLens" "C:\Program Files ( ...

  10. WinForms 实现气泡提示窗口(转载)

    [实例说明] 气泡提示因为他的美观又好被大多数用户所接收,用户所喜爱的就是程序员要实现的. 本实例实现了任务栏气泡提示,运行本实例,效果图如下所示: 单击提示.气泡提示就会显示,单击“关闭”气泡又会消 ...