Skip to analysis

*This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!*

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.

Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

The distance between any two farms will not exceed 100,000.

### PROGRAM NAME: agrinet

### INPUT FORMAT

Lines 1: |
The number of farms, N (3 <= N <= 100). |

Line 2..end: |
The subsequent lines contain the N x N connectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem. |

### SAMPLE INPUT (file agrinet.in)

```
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
```

### OUTPUT FORMAT

The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

### SAMPLE OUTPUT (file agrinet.out)

```
28
```

## CODE

## Java

## C++

## Pascal

## ANALYSIS

##
Russ Cox

This problem requires finding the minimum spanning tree of the given graph. We use an algorithm that, at each step, looks to add to the spanning tree the closest node not already in the tree.

Since the tree sizes are small enough, we don’t need any complicated data structures: we just consider every node each time.

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAXFARM 100
int nfarm;
int dist[MAXFARM][MAXFARM];
int isconn[MAXFARM];
void
main(void)
{
FILE *fin, *fout;
int i, j, nfarm, nnode, mindist, minnode, total;
fin = fopen("agrinet.in", "r");
fout = fopen("agrinet.out", "w");
assert(fin != NULL && fout != NULL);
fscanf(fin, "%d", &nfarm);
for(i=0; i<nfarm; i++)
for(j=0; j<nfarm; j++)
fscanf(fin, "%d", &dist[i][j]);
total = 0;
isconn[0] = 1;
nnode = 1;
for(isconn[0]=1, nnode=1; nnode < nfarm; nnode++) {
mindist = 0;
for(i=0; i<nfarm; i++)
for(j=0; j<nfarm; j++) {
if(dist[i][j] && isconn[i] && !isconn[j]) {
if(mindist == 0 || dist[i][j] < mindist) {
mindist = dist[i][j];
minnode = j;
}
}
}
assert(mindist != 0);
isconn[minnode] = 1;
total += mindist;
}
fprintf(fout, "%d\n", total);
exit(0);
}
```

##
Alex Schwendner

Here is additional analysis from Alex Schwendner.

The solution given is O(N^3); however, we can obtain O(N^2) if we modify it by storing the distance from each node outside of the tree to the tree in an array, instead of recalculating it each time. Thus, instead of checking the distance from every node in the tree to every node outside of the tree each time that we add a node to the tree, we simply check the value in the array for each node outside of the tree.

```
#include <fstream.h>
#include <assert.h>
const int BIG = 20000000;
int n;
int dist[1000][1000];
int distToTree[1000];
bool inTree[1000];
main ()
{
ifstream filein ("agrinet.in");
filein >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
filein >> dist[i][j];
}
distToTree[i] = BIG;
inTree[i] = false;
}
filein.close ();
int cost = 0;
distToTree[0] = 0;
for (int i = 0; i < n; ++i) {
int best = -1;
for (int j = 0; j < n; ++j) {
if (!inTree[j]) {
if (best == -1 || distToTree[best] > distToTree[j]) {
best = j;
}
}
}
assert (best != -1);
assert (!inTree[best]);
assert (distToTree[best] < BIG);
inTree[best] = true;
cost += distToTree[best];
distToTree[best] = 0;
for (int j = 0; j < n; ++j) {
if (distToTree[j] > dist[best][j]) {
distToTree[j] = dist[best][j];
assert (!inTree[j]);
}
}
}
ofstream fileout ("agrinet.out");
fileout << cost << endl;
fileout.close ();
exit (0);
}
```

Back to top