Nanyang Institute of Technology ACM Dole race cum 16 retired Memorial Tournament B PK blowing bubbles

Topic Source: http://acm.nyist.edu.cn/problem/1662

Subject description:

After a day of training, PK ready to relax, blew bubbles on the lawn. After a meal of PK operations, appeared in the sky of N bubbles, each bubble are numbered. M a given relationship, each relationship given in uvw, expressed as the number of bubbles may be u v and number of bubbles together, and the cost spent for w.
At this time, PK thought of a question: how to minimize the cost of these n bubbles in some of the bubbles together, so that a bubble k Unicom block appears in the sky? But PK immersed in a sea of bubbles can not extricate themselves, do not want to think about this question, he wants you to back him answer.

Enter a description:

The first row of three integers n, m and k (1 <= k <= n <= 1000, 1 <= m <= 500000). It represents the number of bubbles, PK-correlation analysis and the final bubble Unicom number of blocks to be formed.
The next m lines of three numbers u, v, w (1 < = u, v <= n, 0 <w <= 1000000), that you can put bubbles u and v together bubble, the cost it takes to w.

Output Description:

Output an integer, n represents this number of bubbles in the bubble together such that minimum cost k blocks Unicom bubbles appear in the sky.

Sample input:
4 4 2
1 2 1
2 3 4
3 4 2
1 4 5
Sample output:
3

Messing disjoint-set (kruskal and engage them almost
in the initial state of n Unicom seen blocks, adding to the two sides of eleven blocks does not Unicom Unicom, Unicom regarded as the number of blocks (i) -1.
Of row-side after line sorting with maintained with disjoint-set relationship Unicom block, until only the K blocks Unicom. (k points BREAK not added, but the rest of the sky when the k-th link that is out fast when combined judgment. (Details look at the code

Reference Code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#define ll long long
const int N=1e6+5;
#define inf 0x3f3f3f3f
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
int pre[N],n,m,k,num;
struct node
{
   int u,v,w;
} mmp[500005];
bool cmp(node a,node b)
{
       return a.w<b.w;
}
void init()
{
   for(int i=1; i<=n; i++)
       pre[i]=i;
}
int Find(int x)
{
   return x==pre[x]?x:pre[x]=Find(pre[x]);
}
bool Join(int x,int y)
{
   int fx=Find(x),fy=Find(y);
   if(fx!=fy)
   {
       num--;
       pre[fy]=fx;
       return 1;
   }
   return 0;
}
int kruskal(){
   int sum=0;
   for(int i=0;i<m;i++) {
       if (Join(mmp[i].u, mmp[i].v)) {
           sum += mmp[i].w;
       }
       if (num==k)
           break;
   }
   return sum;
}
int main()
{
    //freopen("//home/nuoyanli//文档//0615//B//data//secret//3.in", "rep", stdin);
   cin>>n>>m>>k;
   num=n;
   init();
   for(int i=0; i<m; i++)
   {
       int a,b,c;
       cin>>a>>b>>c;
       mmp[i].u=a,mmp[i].v=b,mmp[i].w=c;
   }
   sort(mmp,mmp+m,cmp);
   cout<<kruskal()<<endl;
   return 0;
}
/**
4 4 2
1 2 1
2 3 4
3 4 2
1 4 5
3
4 3 2
1 2 1
2 3 2
3 4 1
2
5 3 2
1 2 1
2 3 2
3 4 1

**/

Guess you like

Origin blog.csdn.net/nuoyanli/article/details/92445259