Constructing Roads-最小生成树(kruskal)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102

题目描述:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 struct node
 7 {
 8     int u,v,cost;
 9 }a[10005];
10 int pre[105];
11 int fin(int x)
12 {
13     if(x==pre[x])
14     {
15         return x;
16     }
17     else
18     {
19         return pre[x]=fin(pre[x]);
20     }
21 }
22
23 void join(int x,int y)
24 {
25     int t1=fin(x);
26     int t2=fin(y);
27     if(t1!=t2)
28     {
29         pre[t1]=t2;
30     }
31 }
32
33 bool cmp(node x,node y)
34 {
35     return x.cost<y.cost;
36 }
37
38 int main()
39 {
40     int n;
41     while(~scanf("%d",&n))
42     {
43         for(int i=0;i<=n;i++)
44         {
45             pre[i]=i;
46         }
47         int num,cnt=0;;
48         for(int i=1;i<=n;i++)
49         {
50             for(int j=1;j<=n;j++)
51             {
52                 scanf("%d",&num);
53                 a[cnt].u=i;
54                 a[cnt].v=j;
55                 a[cnt].cost=num;
56                 cnt++;
57             }
58         }
59         sort(a,a+cnt,cmp);
60         int sum1=0,sum=0;//此处算是一个小剪枝吧
61         int q;
62         scanf("%d",&q);
63         int c,d;
64         for(int i=0;i<q;i++)
65         {
66             scanf("%d%d",&c,&d);
67             if(fin(c)!=fin(d))
68             {
69                 join(c,d);//已经修好路的村庄链接成一个集合
70                 sum1++;
71             }
72         }
73         for(int i=0;i<cnt;i++)
74         {
75             if(fin(a[i].u)!=fin(a[i].v))
76             {
77                 join(a[i].u,a[i].v);
78                 sum+=a[i].cost;
79                 sum1++;
80             }
81             if(sum1==n-1)
82             {
83                 break;
84             }
85         }
86         printf("%d\n",sum);
87     }
88     return 0;
89 }

原文地址:https://www.cnblogs.com/LJHAHA/p/10344037.html

时间: 2024-10-16 01:31:34

Constructing Roads-最小生成树(kruskal)的相关文章

HDU 1102 Constructing Roads (最小生成树)

最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1表示原图不连通 */ const int INF = 0x3f3f3f3f; const int MAXN = 110; bool vis[MAXN]; int lowc[MAXN]; int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n) {

POJ 2421 Constructing Roads (最小生成树)

Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2421 Appoint description:  System Crawler  (2015-05-27) Description There are N villages, which are numbered from 1 to N, and y

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目: Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207 Accepted Submission(s): 135   Problem Description There are N villages, which are numbered from 1 to N, and you should bu

hdu 1102 Constructing Roads 最小生成树Prim算法AC 水~~~

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15230    Accepted Submission(s): 5826 Problem Description There are N villages, which are numbered from 1 to N, and you should

POJ - 2421 Constructing Roads (最小生成树)

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a

hdu 1102 Constructing Roads(最小生成树 Prim)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

HDU 1102 Constructing Roads【简单最小生成树,Prime算法+Kruskal算法】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20765    Accepted Submission(s): 7934 Problem Description There are N villages, which are numbered from 1 to N, and you should

POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 8315 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each

POJ 2421 Constructing Roads(最小生成树)

题意  在n个村庄之间修路使所有村庄连通  其中有些路已经修好了  求至少还需要修多长路 还是裸的最小生成树  修好的边权值为0就行咯 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N = 105, M = 10050; int par[N], n, m, mat[N][N]; int ans; str