usaco_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: guangxue1  
PROG: cowtour 
LANG: C++  
*/
#include <fstream>
#include <math.h>
using namespace std;

int cnt;
int color=-1;
double color_dia[150];
double distance1[150][150];
struct Point
{
 double x;
 double y;
 int color;         
};
Point map[150];
char relation[150][150];

  ofstream fout("cowtour.out");
double min(double a, double b)
{return a>b?b:a;}
double max(double a, double b)
{return a>b?a:b;}
void input()
{
 for(int i=0; i<150; ++i)
    map[i].color=-1;
 ifstream fin("cowtour.in");
 fin>>cnt;
 for(int i=0; i<cnt; ++i)
  fin>>map[i].x>>map[i].y;
 for(int i=0; i<cnt; ++i)
   for(int j=0; j<cnt; ++j)
       fin>>relation[i][j];
}

void drawcolor(int a,int color)
{
	if(map[a].color!=-1) {return;}
    map[a].color=color;
  for(int i=0; i<cnt; ++i)
	  if(relation[a][i]=='1')
	  {  

            distance1[a][i]=sqrt((map[a].x-map[i].x)*(map[a].x-map[i].x)+(map[a].y-map[i].y)*(map[a].y-map[i].y));
            distance1[i][a]=distance1[a][i];
			drawcolor(i,color);
	  }
}


void floyd()
{
  for(int i=0; i<cnt; ++i)
     for(int j=0; j<cnt; ++j)
         if(distance1[i][j]==0)
             distance1[i][j]=100000;
   for(int k=0;k<cnt;++k)
     for(int i=0;i<cnt;++i)
        for(int j=i+1;j<cnt;++j)
           {                             
                    distance1[i][j]=min(distance1[i][j],distance1[i][k]+distance1[k][j]);
					distance1[j][i]=distance1[i][j];                 
            }
   for(int i=0; i<cnt; ++i)
      for(int j=i+1; j<cnt; ++j)
           if(map[i].color==map[j].color)
              if(color_dia[map[i].color]<distance1[i][j])
                     color_dia[map[i].color]=distance1[i][j];
  
}

double dia=100000;

void complete()
{
  int path=0;
  for(int i=0; i<cnt; ++i)
    for(int j=i+1; j<cnt; ++j)
      if(map[i].color!=map[j].color)
        {
          double max_a=0,max_b=0;
          for(int k=0; k<cnt; ++k)
              if(map[i].color==map[k].color&&i!=k)
                  if(distance1[i][k]>max_a)
                          max_a=distance1[i][k];
          for(int k=0; k<cnt; ++k)
              if(map[j].color==map[k].color&&j!=k)
                  if(distance1[j][k]>max_b)
                          max_b=distance1[j][k];
          double increat=sqrt((map[i].x-map[j].x)*(map[i].x-map[j].x)+(map[i].y-map[j].y)*(map[i].y-map[j].y));
          dia=min(dia,max(max_a+max_b+increat,max(color_dia[map[i].color],color_dia[map[j].color])));
        }
  fout.setf(ios::fixed,ios::floatfield);
  fout.precision(6);
  fout<<dia<<endl;
}

int main()
{
  input();
  for(int i=0; i<cnt; ++i)
  if(map[i].color==-1)
  drawcolor(i,++color); 
  floyd();
  complete();
  return 0;
}

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>

#define INF    (1e40)

typedef struct Point Point;
struct Point {
    int x, y;
};

#define MAXPASTURE 150

int n;
double dist[MAXPASTURE][MAXPASTURE];
double diam[MAXPASTURE];
double fielddiam[MAXPASTURE];
Point pt[MAXPASTURE];
int field[MAXPASTURE];
int nfield;

double
ptdist(Point a, Point b)
{
    return sqrt((double)(b.x-a.x)*(b.x-a.x)+(double)(b.y-a.y)*(b.y-a.y));
}

/* mark the field containing pasture i with number m */
void
mark(int i, int m)
{
    int j;
    if(field[i] != -1) {
        assert(field[i] == m);
        return;
    }

    field[i] = m;
    for(j=0; j<n; j++)
        if(dist[i][j] < INF/2)
            mark(j, m);
}

void
main(void)
{
    FILE *fin, *fout;
    int i, j, k, c;
    double newdiam, d;

    fin = fopen("cowtour.in", "r");
    fout = fopen("cowtour.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d/n", &n);
    for(i=0; i<n; i++)
        fscanf(fin, "%d %d/n", &pt[i].x, &pt[i].y);
        
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            c = getc(fin);
            if(i == j)
                dist[i][j] = 0;
            else if(c == '0')
                dist[i][j] = INF;        /* a lot */
            else
                dist[i][j] = ptdist(pt[i], pt[j]);
        }
        assert(getc(fin) == '/n');
    }

    /* Floyd-Warshall all pairs shortest paths */
    for(k=0; k<n; k++)
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
        if(dist[i][k]+dist[k][j] < dist[i][j])
            dist[i][j] = dist[i][k]+dist[k][j];

    /* mark fields */
    for(i=0; i<n; i++)
        field[i] = -1;
    for(i=0; i<n; i++)
        if(field[i] == -1)
            mark(i, nfield++);

    /* find worst diameters involving pasture i, and for whole field */
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++)
            if(diam[i] < dist[i][j] && dist[i][j] < INF/2)
                diam[i] = dist[i][j];
        if(diam[i] > fielddiam[field[i]])
            fielddiam[field[i]] = diam[i];
    }

    /* consider a new path between i and j */
    newdiam = INF;
    for(i=0; i<n; i++)
    for(j=0; j<n; j++) {
        if(field[i] == field[j])
            continue;

        d = diam[i]+diam[j]+ptdist(pt[i], pt[j]);
        if(d < fielddiam[field[i]])
            d = fielddiam[field[i]];
        if(d < fielddiam[field[j]])
            d = fielddiam[field[j]];
    
        if(d < newdiam)
            newdiam = d;
    }

    fprintf(fout, "%.6lf/n", newdiam);
    exit(0);
}

 

你可能感兴趣的:(input,Path,constraints,output,pair,distance)