HDU - 2122 Ice_cream’s world III

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2122

最小生成树问题,可采用Kruskal算法,贪心策略,每次选取无向带权图的最短边,并把两端点用

并查集的方式添加到一个集合内。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 const int maxn=1000+5,maxm=10000+10;
 5 int u[maxm],v[maxm],w[maxm],r[maxm],p[maxn];
 6 int cmp(const int i,const int j) { return w[i]<w[j];}
 7 int find_parent(int x)
 8 {
 9     if(p[x]==-1)
10         return x;
11     p[x]=find_parent(p[x]);
12     return p[x];
13 }
14 int main()
15 {
16     int n,m;
17     while(scanf("%d%d",&n,&m)!=EOF){
18         for(int i=1;i<=m;i++){
19             scanf("%d%d%d",&u[i],&v[i],&w[i]);
20             r[i]=i;
21         }
22         memset(p,-1,sizeof(p[0])*n);
23         std::sort(r+1,r+m+1,cmp);
24         int anscost=0;
25         for(int i=1;i<=m;i++){
26             int e=r[i],up=find_parent(u[e]),vp=find_parent(v[e]);
27             if(up!=vp){
28                 p[vp]=up;
29                 anscost+=w[e];
30             }
31         }
32         int flag=0;
33         for(int i=0;i<n;i++)
34             if(p[i]==-1){
35                 flag++;
36             }
37         if(flag>1)
38             printf("impossible\n");
39         else
40             printf("%d\n",anscost);
41         printf("\n");
44     }
45     return 0;
46 }

HDU - 2122 Ice_cream’s world III

时间: 2024-07-31 00:50:22

HDU - 2122 Ice_cream’s world III的相关文章

hdu 2122 Ice_cream’s world III(最小生成树)

感觉就是 畅通工程的改版 直接贴代码了 #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MA

HDU 2122 Ice_cream’s world III【最小生成树】

解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1032    Accepted Submission(s): 335 Problem Description ice_cream’s wo

hdoj 2122 Ice_cream’s world III 【最小生成树】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1254    Accepted Submission(s): 414 Problem Description ice_cream's world becomes stronger and stronger; every road is buil

hdoj 2122 Ice_cream’s world III【最小生成树】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1237    Accepted Submission(s): 408 Problem Description ice_cream's world becomes stronger and stronger; every road is buil

hdoj 2122 Ice_cream’s world III

并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1121    Accepted Submission(s): 365 Problem Description ice_cream’s world becomes stronger and stronger; every roa

hdu 2122(Ice_cream’s world III)(最小生成树,两种算法都可以)

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 926    Accepted Submission(s): 303 Problem Description ice_cream's world becomes stronger and stronger; every road is built

A - Ice_cream’s world III

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2122 Description ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world;

HDU2122 Ice_cream’s world III【Kruskal】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 997    Accepted Submission(s): 321 Problem Description ice_cream's world becomes stronger and stronger; every road is built

hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用

3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配),再修改起点还有终点的边流量,继续增广,直到达不到完美匹配为止.网上很多是用二分做的,我觉得没必要...(网上传播跟风真严重...很多人都不是真正懂最大流算法的...) 3277 : 再附加一条件,每个女孩可以最多与k个自己不喜欢的男孩.求有几种完美匹配(同上). 我觉得:求出上题答案,直接ans