USACO 2.4 Cow Tours

Posted sigongzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

————————————————————————题解
应该是简单的最短路……以及检查连通性的并查集
然后就不难了……毕竟只是加一条路而已
要注意的是:接完一条路之后的两个联通块的最长路径最小,这个值可能会比不上某个单独的联通块的最长路径。
嗯。
 1 /*
 2 ID: ivorysi
 3 PROG: cowtour
 4 LANG: C++ 
 5 */
 6 
 7 #include <iostream>
 8 #include <string.h>
 9 #include <cstdlib>
10 #include <cstdio>
11 #include <algorithm>
12 #include <cstring>
13 #include <vector>
14 #include <ctime>
15 #include <cmath>
16 #include <queue>
17 #define ivorysi
18 #define mo  1000000007
19 #define siji(i,x,y) for(int i=(x);i<=(y);i++)
20 #define gongzi(j,x,y) for(int j=(x);j>=(y);j--)
21 #define xiaosiji(i,x,y) for(int i=(x);i<(y);i++)
22 #define sigongzi(j,x,y) for(int j=(x);j>(y);j--)
23 #define ivory(i,x) for(int i=head[x];i;i=edge[i].n)
24 #define pii pair<int,int>
25 #define fi first
26 #define se second
27 #define inf 0x5f5f5f5f
28 #define N 5005
29 typedef long long ll;
30 using namespace std;
31 pii poi[155];
32 char adj[155][155];
33 double leng[155][155];
34 int id1[155],id2[155],cnt1,cnt2;
35 int n,fa[155];
36 int op[155];
37 double ans=10000006;
38 int powt(int a) {return a*a;}
39 int getfa(int x) {return fa[x]==x?x:fa[x]=getfa(fa[x]);}
40 int main(int argc, char const *argv[])
41 {
42 #ifdef ivorysi
43     freopen("cowtour.in","r",stdin);
44     freopen("cowtour.out","w",stdout);
45 #else
46     freopen("f1.in","r",stdin);
47 #endif
48     scanf("%d",&n);
49     siji(i,1,n) {
50         scanf("%d %d",&poi[i].fi,&poi[i].se);
51     }
52     getchar();
53     siji(i,1,n) fa[i]=i;
54     siji(i,1,n) {
55         siji(j,1,n) {
56             leng[i][j]=10000005;
57         }
58     }
59     siji(i,1,n) leng[i][i]=0;
60     siji(i,1,n) {
61         scanf("%s",adj[i]+1);
62         siji(j,1,n) {
63             if(adj[i][j]==1) {
64                 fa[getfa(i)]=getfa(j);
65                 leng[i][j]=sqrt((double)powt(poi[i].fi-poi[j].fi)+powt(poi[i].se-poi[j].se));
66             }
67         }
68     }
69     siji(k,1,n) {
70         siji(i,1,n) {
71             siji(j,1,n) {
72                 leng[i][j]=min(leng[i][k]+leng[k][j],leng[i][j]);
73             }
74         }
75     }
76     siji(i,1,n) op[i]=i;
77     siji(i,1,n) {
78         siji(j,1,n) {
79             if(i!=j && leng[i][j]<10000000 ) {
80                 if(leng[i][op[i]]<leng[i][j]) op[i]=j;
81             }
82         }
83     }
84     siji(i,1,n) {
85         siji(j,1,n) {
86             if(getfa(i)!=getfa(j)) {
87                 double tmp=sqrt((double)powt(poi[i].fi-poi[j].fi)+powt(poi[i].se-poi[j].se));
88                 tmp=tmp+leng[i][op[i]]+leng[j][op[j]];
89                 ans=min(ans,tmp);
90             }
91         }
92     }
93     siji(i,1,n) {
94         ans=max(ans,leng[i][op[i]]);//单独的联通块里的最大值过一遍
95     }
96     printf("%.6lf\n",ans);
97 }

 

 

以上是关于USACO 2.4 Cow Tours的主要内容,如果未能解决你的问题,请参考以下文章

[USACO][最短路]Cow Tours

P1522 牛的旅行 Cow Tours

Luogu P1522 牛的旅行 Cow Tours

P2419 [USACO08JAN]牛大赛Cow Contest

bzoj3893[Usaco2014 Dec]Cow Jog*

P2863 [USACO06JAN]牛的舞会The Cow Prom