zoukankan      html  css  js  c++  java
  • USACO Section 2.4 Cow Tours (cowtour)

    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
    

    思路:先一次floyd,在对每个点找出由这个点可达到的最大值,最后枚举每个片区,加上算出最小值就对了
    Executing...
       Test 1: TEST OK [0.000 secs, 3480 KB]
       Test 2: TEST OK [0.000 secs, 3480 KB]
       Test 3: TEST OK [0.000 secs, 3480 KB]
       Test 4: TEST OK [0.000 secs, 3480 KB]
       Test 5: TEST OK [0.011 secs, 3480 KB]
       Test 6: TEST OK [0.011 secs, 3480 KB]
       Test 7: TEST OK [0.011 secs, 3480 KB]
       Test 8: TEST OK [0.011 secs, 3480 KB]
       Test 9: TEST OK [0.011 secs, 3480 KB]
    
    All tests OK.
      1 /*
      2 ID:wuhuaju2
      3 PROG:cowtour
      4 LANG:C++
      5 */
      6 
      7 #include <cstdio>
      8 #include <iostream>
      9 #include <cstdlib>
     10 #include <algorithm>
     11 #include <cstring>
     12 #include <cmath>
     13 using namespace std;
     14 
     15 const int maxn=160;
     16 const double INF=1E8;
     17 int n,a[maxn],b[maxn];
     18 double d[maxn][maxn],dmax[maxn],ans;
     19 char s[maxn][maxn];
     20 bool f[maxn][maxn];
     21 
     22 void close()
     23 {
     24     fclose(stdin);
     25     fclose(stdout);
     26     exit(0);
     27 }
     28 
     29 double dis(int x1,int y1,int x2,int y2)
     30 {
     31     return sqrt(abs(x2-x1)*abs(x2-x1)+abs(y1-y2)*abs(y1-y2));
     32 }
     33 
     34 
     35 void work()
     36 {
     37     for (int k=1;k<=n;k++)
     38     {
     39         for (int i=1;i<=n;i++)
     40         {
     41             for (int j=1;j<=n;j++)
     42                 if (d[i][k]+d[k][j]<d[i][j])
     43                 {
     44                     d[i][j]=d[i][k]+d[k][j];
     45                 }
     46         }
     47     }
     48     memset(dmax,0,sizeof(dmax));
     49     for (int i=1;i<=n;i++)
     50     {
     51         for (int j=1;j<=n;j++)
     52         {
     53             if (i!=j && d[i][j]!=INF && dmax[i]<d[i][j])
     54                 dmax[i]=d[i][j];
     55         }
     56     }
     57     /*
     58     for (int i=1;i<=n;i++)
     59     {
     60         printf("%d %d %lf\n",a[i],b[i],dmax[i]);
     61     }
     62     */
     63     ans=INF*INF;
     64     for (int i=1;i<=n;i++)
     65     {
     66         for (int j=1;j<=n;j++)
     67         {
     68             if (d[i][j]==INF)
     69             {
     70                 if (dis(a[i],b[i],a[j],b[j])+dmax[i]+dmax[j]<ans)
     71                     ans=dis(a[i],b[i],a[j],b[j])+dmax[i]+dmax[j];
     72             }
     73         }
     74     }
     75     for (int i=1;i<=n;i++)
     76         if (dmax[i]>ans) ans=dmax[i];
     77     printf("%.6lf\n",ans);
     78 }
     79 
     80 void init ()
     81 {
     82 freopen("cowtour.in","r",stdin);
     83 freopen("cowtour.out","w",stdout);
     84   scanf("%d",&n);
     85   for (int i=1;i<=n;i++)
     86       scanf("%d%d",&a[i],&b[i]);
     87   for (int i=1;i<=n;i++)
     88   {
     89       scanf("%s",s[i]);
     90       for (int j=1;j<=n;j++)
     91       {
     92           if (s[i][j-1]=='1')
     93           {
     94               d[i][j]=dis(a[i],b[i],a[j],b[j]);
     95               f[i][j]=true;
     96           }
     97           else d[i][j]=INF;
     98           if (i==j) d[i][j]=0;
     99       }
    100   }
    101 }
    102 
    103 int main ()
    104 {
    105     init();
    106     work();
    107     close();
    108     return 0;
    109 }
  • 相关阅读:
    常见数据结构和算法 的可视化
    JSON与XML
    JavaScript 中的陷阱
    C++ primer(十三)--类继承、构造函数成员初始化、虚函数、抽象基类
    mongodb学习(二)
    再谈怎样以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
    LaTeX Subfigure 中间加入垂直线
    JAVA基础针对自己薄弱环节总结02(循环)
    软考之路--用文字记录这个漂亮的进程
    mysql异常Lock wait timeout exceeded; try restarting transaction
  • 原文地址:https://www.cnblogs.com/cssystem/p/2883799.html
Copyright © 2011-2022 走看看