HDU 1233 is still a smooth project

                                          or smooth engineering

A provincial survey of rural traffic conditions, the obtained statistical table lists the distance between any two villages. The goal of the provincial government's "unblocked project" is to enable road traffic between any two villages in the province (but not necessarily directly connected by road, as long as it can be reached indirectly by road), and requires the total length of the road to be paved to the minimum. Please calculate the minimum total road length.  
Input test input contains several test cases. The first line of each test case gives the number of villages N ( < 100 ); the subsequent N(N-1)/2 lines correspond to the distance between villages, and each line gives a pair of positive integers, which are the two villages number, and the distance between the two villages. For simplicity, villages are numbered from 1 to N.  
When N is 0, the input ends and the use case is not processed.  
Output For each test case, output the minimum total road length on 1 line.  
Sample Input
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
Sample Output
3
5


        
  
Huge input, scanf is recommended.

 Kruskal for a bare minimum spanning tree

code show as below:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
using namespace std;
struct node
{
    int u,v,w;
    bool operator <( const node &a)
    {
        return w<a.w;
    }
} s[101*50];
int pre[105];
int Find(int x)
{
    return x==pre[x]?x:pre[x]=Find(pre[x]);
}
int main()
{
    int n,i;
    while(scanf("%d",&n),n)
    {
        int m=n*(n-1)/2;
        for(i=0; i<m; i++)
        {
            scanf("%d %d %d",&s[i].u,&s[i].v,&s[i].w);
        }
        for(i=0;i<=n;i++)
            pre[i]=i;
        sort(s,s+m);
        int sum=0;
        for(i=0;i<m;i++)
        {
            int fx=Find(s[i].u),fy=Find(s[i].v);
            if(fx!=fy)
            {
                  pre[fx]=fy;
                  sum+=s[i].w;
            }
        }
        cout<<sum<<endl;

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326303973&siteId=291194637