POJ 1258 agri-net "MST"

Source: Internet
Author: User

Agri-net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 49389 Accepted: 20511

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

4

0 4 9 21

4 0 8 17

9 8 0 16

21 17 16 0

Sample Output

28


John wants to connect N farms together and ask for the minimum cost. The value of the adjacency matrix is given and the MST is obtained.

Idea: greedy Kruskal algorithm AC

Code:

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int map[150][150];int f[150];struct path{int x,y,w;} A[100*100*2];int cmp (path A,path b) {return A.W&LT;B.W;}    int find (int a) {int r=a;    while (F[R]!=R) r=f[r];    int i=a;    Int J;        while (i!=r) {j=f[i];        F[i]=r;    I=j; } return R;}    void merge (int a,int b) {int A, B;    A=find (a);    B=find (b); if (a!=b) f[b]=a;}    int main () {int n;        while (~SCANF ("%d", &n)) {int cont=0;        for (int i=0;i<n;i++) {f[i]=i;                } for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {int k;                scanf ("%d", &k);                A[cont].x=i;                A[cont].y=j;            A[cont++].w=k;        }} int output=0;        Sort (a,a+cont,cmp);   for (int i=0;i<cont;i++) {if (Find (a[i].x)!=find (A[I].Y)) {             Merge (A[I].X,A[I].Y);            OUTPUT+=A[I].W;    }} printf ("%d\n", output); }}





POJ 1258 agri-net "MST"

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.