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.
Line 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. |
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
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.
28 /* ID: guangxue1 PROG: agrinet LANG: C++ */ #include <fstream> using namespace std; int dis_sum; int dis[100][100]; int point[100]; int N; int A,B; int MIN_DIS=999999; void fnext() { MIN_DIS=999999; int tempi,tempj; for(int i=0; i<N; ++i) for(int j=0; j<N; ++j) if(point[i]&&!point[j]&&dis[i][j]&&MIN_DIS>dis[i][j]) {MIN_DIS=dis[i][j]; tempj=j; tempi=i;} dis[tempi][tempj]=dis[tempj][tempi]=0; point[tempj]=1; dis_sum+=MIN_DIS; } int main() { ifstream fin("agrinet.in"); ofstream fout("agrinet.out"); fin>>N; for(int i=0; i<N; ++i) for(int j=0; j<N; ++j) {fin>>dis[i][j]; if(MIN_DIS>dis[i][j]&&(i-j)) MIN_DIS=dis[i][j], A=i,B=j;} dis_sum+=dis[A][B]; point[A]=1,point[B]=1; dis[A][B]=dis[B][A]=0; for(int i=0; i<N-2; ++i) fnext(); fout<<dis_sum<<endl; return 0; }
#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); }
#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); }