部落冲突

这里写图片描述
这题弄k各部落
就是把它合并成k个集合
所以按最小的边合并的解一定是最优的
这就借用了k算法的思想

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
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct bbb{
int x,y;
int s;
};
bbb p[1000000+100];
int f[1999],s[1999];

int find(int x)
{
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
int cmp(const bbb a,const bbb b)
{
if(a.s<b.s)
return 1;
return 0;
}
int t,ans;
int n,m,k;
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].s);
for(int i=1;i<=n;i++)
f[i]=i;
sort(p+1,p+m+1,cmp);

for(int i=1;i<=n-k;i++)
{
int fx=1,fy=1;
while(fx==fy){
++t;
if(t>m) {
printf("No Answer");
return 0;
}
fx=find(p[t].x),fy=find(p[t].y);
}

f[fx]=fy;
ans+=p[t].s;
}
printf("%d",ans);
}

最近的文章

day4

alien飞船降落问题矩阵前缀和+枚举求出前缀和,暴力枚举每一个子矩阵的和,是0就可以降落注意有正方形飞船,长度不等于点123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495 …

于  2016~2017 继续阅读
更早的文章

cicada

正解还不会用暴力搜索弄到了90.。。bfs与dfs都能过90思路:先弄一个f数组记得是这个点的最短路,就是破败指针在爆搜剪枝bfs1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 …

于  搜索 继续阅读