zoukankan      html  css  js  c++  java
  • 2.4.3 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
    
    /*
    ID: makeeca1
    PROG: cowtour
    LANG: C++
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #define MAXN 160
    #define min(x,y) ((x>y)?y:x)
    using namespace std;
    const double INF=1E15;
    int n,father[MAXN],x[MAXN],y[MAXN];
    double nmax[MAXN],dist[MAXN][MAXN],ans;
    char c;
    int find(int x){
        if (x==father[x])return x;
        else return father[x]=find(father[x]);
    }
    
    void merge(int x,int y){
        int xx=find(x);
        int yy=find(y);
        if (xx==yy) return;
        if (xx<yy) father[yy]=father[xx];
        else father[xx]=father[yy];
    }
    int main(){
        freopen("cowtour.in","r",stdin);
        freopen("cowtour.out","w",stdout);
        ios::sync_with_stdio(false);
        scanf("%d",&n);
        for (int i=0;i<n;i++){
            father[i]=i;nmax[i]=-1;
            scanf("%d%d",&x[i],&y[i]);
        }
        for (int i=0;i<n;i++){
            getchar();
            for (int j=0;j<n;j++){
                c=getchar();
                if (i==j) {dist[i][j]=0;continue;}
                if (c=='1'){
                    dist[i][j]=sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
                    merge(i,j);
                }
                else dist[i][j]=INF; 
            }
        }
        for (int k=0;k<n;k++)
            for (int i=0;i<n;i++)
                for (int j=0;j<n;j++){
                    if (k==i || k==j) continue;
                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
                }
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                if (dist[i][j]!=INF && dist[i][j]>nmax[i]) nmax[i]=dist[i][j];
        ans=INF;
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                if (find(i)!=find(j)&& dist[i][j]==INF){
                    double dd=sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
                    ans=min(ans,dd+nmax[i]+nmax[j]);
                }
        for (int i=0;i<n;i++)if (nmax[i]>ans) ans=nmax[i];    
        printf("%.6lf
    ",ans);
        return 0;
    }
  • 相关阅读:
    Jquery 添加插件
    后台添加前台标签
    jQuery.validate 中文API
    jquery validate 详解二
    jquery validate 详解一
    System.Collections里的一些接口
    C#中 Reference Equals, == , Equals的区别
    关于iOS原生条形码扫描,你需要注意的两三事
    layoutSubviews何时调用的问题(转)
    layoutSubviews总结
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274627.html
Copyright © 2011-2022 走看看