USACO Section 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
题意:一个有 n 个牧场,但是有很多个区域,每个区域是强连通的,每个区域的最短路中的最大值定义为直径,现在只添加一条边连接两个区域,求最小的直接。
分析:使用并查集分区域,使用floyd 计算直径。
View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: cowtour
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define MAXN 155
#define INF 0x3fffffff

using namespace std;

struct point
{
    double x,y;
} p[MAXN];
int flag[MAXN][MAXN],fat[MAXN];
double map[MAXN][MAXN],dist[MAXN];

int finds(int a)
{
    if(fat[a]==a)  return a;
    fat[a]=finds(fat[a]);
    return fat[a];
}

void floyd(int n)
{
    int i,j,k;
    for(k=1; k<=n; k++)
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
            }
        }
    }
}

double dist_ab(point a,point b)
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

int main()
{
    freopen("cowtour.in","r",stdin);
    freopen("cowtour.out","w",stdout);
    int n,fi,fj,i,j;
    while(scanf("%d",&n)==1)
    {
        for(i=1; i<=n; i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
            fat[i]=i;
        }
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                map[i][j]=((i==j)?0:INF);
                scanf("%1d",&flag[i][j]);
                if(flag[i][j])
                {
                    map[i][j]=dist_ab(p[i],p[j]);
                    fi=finds(i);fj=finds(j);
                    if(fi!=fj) fat[fi]=fj;
                }
            }
        }
        floyd(n);
        double ans=INF;
        for(i=1;i<=n;i++)
        {
            dist[i]=0;
            for(j=1;j<=n;j++)
            {
                if(map[i][j]!=INF)
                {
                    dist[i]=max(dist[i],map[i][j]);
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(flag[i][j]==0&&fat[i]!=fat[j])
                    ans=min(ans,dist[i]+dist[j]+dist_ab(p[i],p[j]));
            }
        }
        for(i=1;i<=n;i++)
            ans=max(ans,dist[i]);
        printf("%.6lf\n",ans);
    }
    return 0;
}
posted @ 2012-09-07 11:15  mtry  阅读(765)  评论(0编辑  收藏  举报