Russ Cox
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
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. |
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
思路:没的说,典型的最小生成树,我用PRIM算法,从0开始每次加入权值最小的边,然后更新新加点的边。
或用并查集也行,每次选权最小的边,若这两点中有点不在集合中则将这边的两顶点加入集合,直到找不到符合的边
我一次过的,具体看代码:
/*
ID:nealgav1
LANG:C++
PROG:agrinet
*/
#include<fstream>
using namespace std;
ifstream cin("agrinet.in");
ofstream cout("agrinet.out");
const int mm=210;
const int oo=1e9;
int map[mm][mm];//记录边
int dist[mm];//记录割边距离
bool vis[mm];//标记
int m,sum;
void prime()
{
for(int i=0;i<m;i++)
dist[i]=map[0][i];
dist[0]=oo;vis[0]=1;
int i,j,k;
for(i=1;i<m;i++)
{ int mindist=oo;
for(j=0;j<m;j++)//找最小割边加入并标记
if(!vis[j]&&dist[j]<mindist)mindist=dist[j],k=j;
sum+=dist[k];vis[k]=1;
for(j=0;j<m;j++)//更新割边
if(k!=j&&dist[j]>map[k][j])dist[j]=map[k][j];
}
}
int main()
{
cin>>m;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
cin>>map[i][j];
sum=0;
prime();
cout<<sum<<"\n";
}
Executing...
Test 1: TEST OK [0.000 secs, 3528 KB]
Test 2: TEST OK [0.000 secs, 3528 KB]
Test 3: TEST OK [0.000 secs, 3528 KB]
Test 4: TEST OK [0.000 secs, 3528 KB]
Test 5: TEST OK [0.000 secs, 3528 KB]
Test 6: TEST OK [0.000 secs, 3528 KB]
Test 7: TEST OK [0.000 secs, 3528 KB]
Test 8: TEST OK [0.011 secs, 3528 KB]
Test 9: TEST OK [0.011 secs, 3528 KB]
Test 10: TEST OK [0.011 secs, 3528 KB]
All tests OK.
YOUR PROGRAM ('agrinet') WORKED FIRST TIME! That's fantastic
-- and a rare thing. Please accept these special automated
congratulations.
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); }
Here is additional analysis from Alex Schwendner:
The solution given is O(N3); however, we can obtain O(N2) 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); }