floyd-warshall



Hello,

I'm stuck with a horribly simple task: given a graph with more than one component, add such an edge between two components that the newly-created component will have the smallest possible diameter. There are at most 150 points. Detailed description attached below.

Anyone willing to give it a shot and kill a stupid bug? All I can say is "it doesn't work", since this is all the info I get from the online evaluation system (USACO, if you're familiar with it).

Here's my go at it:
================================================
#include <iostream> //cout
#include <fstream> //open
#include <cmath> //sqrt
#include <cstdio> //sprintf
using namespace std;

#define MAXN 150
#define INF 1e6
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

//number of points
int n;

//points' coordinates
int x[MAXN]; int y[MAXN];
//minimal walking distances between each pair of points
double dist[MAXN][MAXN];


//distance between points i and j as the crow flies; Pythagora double airDist(int i, int j) { return sqrt(1.0*(x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); }


int main(int argc, char *argv[]) { ifstream inFile("cowtour.in", ios::in); ofstream outFile("cowtour.out", ios::out);


//read the data:
inFile >> n;
//first the coordinates...
for (int i=0; i<n; i++) {
inFile >> (x[i]) >> (y[i]);
}
//...next the edges of the graph
char line[MAXN];
for (int i=0; i<n; i++) {
inFile >> line;
for (int j=0; j<n; j++) {
//if there's a connection, weigh the edge with its distance, else set it to infinity
dist[i][j] = (line[j] == '1' ? airDist(i,j) : INF);
}
dist[i][i] = 0;
}



//Floyd-Warshall to determine minimal distances between each pair for (int k=0; k<n; k++) { for (int j=0; j<n; j++) { for (int i=0; i<n; i++) { if (dist[i][k]+dist[k][j] < dist[i][j]) { dist[i][j] = dist[j][i] = dist[i][k]+dist[k][j]; } } } }


//in this table we'll store the distance to the farthest point we still can reach (from each point)
double diam[MAXN];
for (int i=0; i<n; i++) {
diam[i]=0;
for (int j=0; j<n; j++) {
if (dist[i][j] != INF) { diam[i] = max(diam[i], dist[i][j]); }
}
}


//for each pair of unconnected points, imagine an edge between them and see if the ensuing diameter
//would be shorter than anything found before
double minDiam = INF;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (dist[i][j] != INF || i==j) continue;
//double m = minDiam;
minDiam = min(minDiam, diam[i]+airDist(i,j)+diam[j]);
//if (m!=minDiam)
//cout << i << "(" << diam[i] <<") --"<<airDist(i,j)<<"-- " << j <<"("<<diam[j]<<") :: "<<minDiam<<endl ;
}
}
sprintf(line, "%0.6f\n", minDiam);
outFile << line;


  inFile.close();
  outFile.close();
  return 0;
}

================================================
Exact formulation of the task (copy-paste):
================================================
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.

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
================================================
.



Relevant Pages

  • A contest problem.. where am i wrong??
    ... Farmer John has a number of pastures on his farm. ... connected by any sequence of cow paths, ... A field's `diameter' is defined to be the largest distance of all the ... connections. ...
    (comp.programming)
  • Re: A contest problem.. where am i wrong??
    ... > some pastures with certain other pastures, ... > connected by any sequence of cow paths, ... > shortest walks between any pair of pastures in the field. ... svaq gur fubegrfg cngu orgjrra nyy cnvef bs gur iregvprf va gur ...
    (comp.programming)