POJ 1258 Agri-net

Source: Internet
Author: User
Tags cmath

Agri-nettime limit:1000msmemory limit:10000kbthis problem'll be judged onPKU. Original id:1258
64-bit integer IO format: %lld Java class name: Main 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.
InputThe 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.OutputFor 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
SourceUsaco 102 Problem Solving: minimum spanning tree ... Kruskal algorithm
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - Const intMAXN =10000; - structarc{ +     intu,v,w; -Arcintx =0,inty =0,intz =0){ +U =x; Av =y; atW =Z; -     } - }; - arc E[MAXN]; - intuf[maxn],n,m; - intFind (intx) { in     if(X! =Uf[x]) -UF[X] =Find (uf[x]); to     returnUf[x]; + } - BOOLcmpConstArc &x,ConstArc &y) { the     returnX.W <Y.W; * } $ intKruskal () {Panax Notoginseng      for(inti =0; I <= N; i++) Uf[i] =i; -Sort (e,e+m,cmp); the     intsum =0; +      for(inti =0; I < m; i++){ A         inttx =Find (e[i].u); the         intTy =Find (E[I].V); +         if(tx! =ty) { -Sum + =E[I].W; $UF[TX] =Ty; $         } -     } -     returnsum; the } - intMain () {Wuyi     inti,j,w,u,v; the      while(~SCANF ("%d",&N)) { -          for(m = i =0; I < n; i++){ Wu              for(j =0; J < N; J + +){ -scanf"%d",&W); About                 if(J > i) e[m++] = Arc (i+1, j+1, W); $             } -         } -printf"%d\n", Kruskal ()); -     } A     return 0; +}
View Code

To a priority queue optimization prim ... Ha ha

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - Const intMAXN =10000; - structArc { +     intto,w; -Arcintx =0,inty =0) { +to =x; AW =y; at     } - }; -Vector<arc>G[MAXN]; -priority_queue< PII, vector< pii >,greater< PII > >Q; - intN,D[MAXN]; - BOOLDONE[MAXN]; in intPrim () { -      for(inti =0; I <= N; i++){ toD[i] =INF; +Done[i] =false; -     } the     intAns = d[1] =0; *      while(!q.empty ()) Q.pop (); $Q.push (Make_pair (d[1],1));Panax Notoginseng      while(!Q.empty ()) { -         intU =Q.top (). Second; the         intW =Q.top (). First; + Q.pop (); A         if(Done[u])Continue; theDone[u] =true; +Ans + =W; -          for(inti =0; I < g[u].size (); i++){ $             if(D[g[u][i].to] >G[U][I].W) { $D[g[u][i].to] =G[U][I].W; - Q.push (Make_pair (g[u][i].w,g[u][i].to)); -             } the         } -     }Wuyi     returnans; the } - intMain () { Wu     inti,j,w; -      while(~SCANF ("%d",&N)) { About          for(i =0; I <= N; i++) g[i].clear (); $          for(i =1; I <= N; i++) { -              for(j =1; J <= N; J + +) { -scanf"%d",&W); -                 if(I! =j) G[i].push_back (Arc (j,w)); A             } +         } theprintf"%d\n", Prim ()); -     } $     return 0; the}
View Code

POJ 1258 Agri-net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.