洛谷 P1195 口袋的天空
作者:互联网
题目传送门
解题思路:
本题直接跑一遍Kruskal,当成功加入n-k条边时即为目标状态。
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 5 using namespace std; 6 7 int n,m,k,fa[1001],sum,ans; 8 struct kkk { 9 int qd,zd,v; 10 }e[10001]; 11 12 bool cmp(kkk a,kkk b) { 13 return a.v < b.v; 14 } 15 16 int find_father(int x) { 17 if(fa[x] == x) return x; 18 return fa[x] = find_father(fa[x]); 19 } 20 21 int main() { 22 scanf("%d%d%d",&n,&m,&k); 23 for(int i = 1;i <= m; i++) 24 scanf("%d%d%d",&e[i].qd,&e[i].zd,&e[i].v); 25 for(int i = 1;i <= n; i++) 26 fa[i] = i; 27 sort(e+1,e+1+m,cmp); 28 for(int i = 1;i <= m; i++) { 29 int be,end; 30 be = e[i].qd; 31 end = e[i].zd; 32 be = find_father(be); 33 end = find_father(end); 34 if(be == end) continue; 35 fa[be] = end; 36 sum++; 37 ans += e[i].v; 38 if(sum == n - k) { 39 printf("%d",ans); 40 return 0; 41 } 42 } 43 printf("No Answer"); 44 return 0; 45 }
标签:口袋,洛谷,int,father,fa,kkk,return,include,P1195 来源: https://www.cnblogs.com/lipeiyi520/p/11258146.html