Agri-netTime
limit:1000MS
Memory Limit:10000KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 1258
Description
Farmer John had been elected mayor of his town! One of his campaign promises is to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for he farm and is going to share he 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 all 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 and farms would not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, n (3 <= n <= 100). The following lines contain the n x n conectivity matrix, where each element is shows the distance from on farm to another. Logically, they is n lines of n space-separated integers. Physically, they is limited in the length to the characters, so some lines continue onto others. Of course, the diagonal would be 0, since the distance from farm I to itself are not interesting for this problem.
Output
For each case, output a single integer length this is the sum of the minimum length of fiber required to connect the Entir e set of farms.
Sample Input
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
Bare minimum spanning tree can be done with Kruskal and prim
Kruskal
#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespacestd;intN,coun;structroad{intf,t; intW;}; Road vil[10000+Ten];intfa[10000+Ten];intCMP (Road A, road B) {returna.w<B.W;}intFindintx) { returnfa[x]==x?x:fa[x]=find (Fa[x]);}intKruskal () {intans=0; for(intI=0; i<coun;i++) fa[i]=i; Sort (Vil,vil+coun,cmp); for(intI=0; i<coun;i++) { intx=find (VIL[I].F); inty=find (VIL[I].T); if(x!=y) {fa[x]=y; Ans+=VIL[I].W; } } returnans;}intMain () {inti,j; while(SCANF ("%d", &n)! =EOF) {Coun=0; for(i=1; i<=n;i++) { for(j=1; j<=n;j++) { intwaste; scanf ("%d",&waste); if(i<j) {Vil[coun].f=i; VIL[COUN].T=J; VIL[COUN].W=waste; Coun++; } } } intans=Kruskal (); printf ("%d\n", ans); } return 0;}View Code
Prim
(The code posted on the net feels obvious Kruskal is relatively simple and rough.)
#include <iostream>using namespacestd;Const intinf=100001;//infinitely LargeintN//Number of farmsintdist[101][101];intPrimvoid){ ints=1; intm=1; BOOLu[101]={false}; U[s]=true; intMin_w; intprim_w=0; intPoint ; intlow_dis[101]; /*Initial*/ for(intI=1; i<=n;i++) Low_dis[i]=inf; /*Prim Algorithm*/ while(true) { if(m==N) Break; Min_w=inf; for(intI=2; i<=n;i++) { if(!u[i] && low_dis[i]>Dist[s][i]) low_dis[i]=Dist[s][i]; if(!u[i] && min_w>Low_dis[i]) {Min_w=Low_dis[i]; Point=i; }} s=Point ; U[s]=true; Prim_w+=Min_w; M++; } returnPrim_w;}intMainvoid){ while(cin>>N) {/*Input*/ for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) Cin>>Dist[i][j]; /*Prim algorithm & Output*/cout<<prim () <<Endl; } return 0;}View Code
POJ 1258 agri-net (minimum spanning tree)