lg1195

链接:lg1195

Description

  • 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空。
  • 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖。
  • 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在一起。
  • 现在小杉要把所有云朵连成K个棉花糖,一个棉花糖最少要用掉一朵云,小杉想知道他怎么连,花费的代价最小。

    Input

  • 每组测试数据的
  • 第一行有三个数N,M,K(1<=N<=1000,1<=M<=10000,1<=K<=10)
  • 接下来M个数每行三个数X,Y,L,表示X云和Y云可以通过L的代价连在一起。(1<=X,Y<=N,0<=L<10000)
  • 30%的数据N<=100,M<=1000

    Output

  • 对每组数据输出一行,仅有一个整数,表示最小的代价。
  • 如果怎么连都连不出K个棉花糖,请输出’No Answer’。

    Sample Input

    3 1 2
    1 2 1

    Sample Output

    1

    题解

  • 建一个包含n-k-1个节点的最小生成树
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,l,n) for(register int i=l;i<n;++i)
#define ll long long
using namespace std;
const int N=1e5+10;
struct node {
int u,v,w;
}e[N];
int n,m,k,ans;
int f[N];
int cmp(const void *a,const void *b){
return ((node*)a)->w-((node*)b)->w;
}

int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}

inline void un(int x,int y){
x=find(x);
y=find(y);
f[x]=f[y];
}

void Kruskal(){
qsort(e,m,sizeof(e[0]),cmp);
rep(i,1,n+1)f[i]=i;
int cnt=0;
rep(i,0,m){
int x=e[i].u,y=e[i].v;
if(find(x)!=find(y)){
un(x,y);
ans+=e[i].w;
cnt++;
}
if(cnt==k)break;
}
}

int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
ans=0;
rep(i,0,m)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
k=n-k;
if(k<=m)Kruskal();
if(ans)printf("%d\n",ans);
else printf("No Answer\n");
}
return 0;
}