hdu1102 Constructing Roads 基础最小生成树

 1 //克鲁斯卡尔(最小生成树)
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn = 100005;
 8 int n, t;
 9 struct node
10 {
11     int bagin, end, len;
12 }arr[maxn];
13 int fa[maxn];
14
15 void init()
16 {
17     for (int i = 0; i <= n; i++)
18     {
19         fa[i] = i;
20     }
21 }
22
23 bool cmp(node a, node b)
24 {
25     return a.len<b.len;
26 }
27
28 int find(int x)
29 {
30     if (x != fa[x])
31         fa[x] = find(fa[x]);
32     return fa[x];
33 }
34
35 int main()
36 {
37     while (cin >> n)
38     {
39         t = 0;
40         for (int i = 1; i <= n; i++)
41         {
42             for (int j = 1; j <= n; j++)
43             {
44                 int x;
45                 cin >> x;
46                 arr[t].bagin = i; arr[t].end = j; arr[t].len = x;
47                 t++;
48             }
49         }
50         sort(arr, arr + t, cmp);
51         init();
52         int m;
53         cin >> m;
54         while (m--)
55         {
56             int a, b;
57             cin >> a >> b;
58             int fx, fy;
59             fx = find(a); fy = find(b);
60             fa[fy] = fx;
61         }
62         int ans = 0;
63         for (int i = 0; i<t; i++)
64         {
65             int fx, fy;
66             fx = find(arr[i].bagin);
67             fy = find(arr[i].end);
68             if (fx != fy)
69             {
70                 ans += arr[i].len;
71                 fa[fy] = fx;
72             }
73         }
74         cout << ans << endl;
75     }
76     return 0;
77 }
时间: 2024-11-05 10:39:25

hdu1102 Constructing Roads 基础最小生成树的相关文章

POJ2421 &amp; HDU1102 Constructing Roads(最小生成树)

嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告诉你每个村庄之间的距离,和几组已经联通的村庄,求使所有村庄联通所要建的道路的最短距离. 很简单,用最小生成树的Prim算法,相当于邻接矩阵已知,把已联通的村庄之间的距离改为零即可. 附上AC代码: 1 #include <stdio.h> 2 #include <string.h> 3

HDU1102 Constructing Roads 【最小生成树Prim】

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

hdu1102 Constructing Roads (简单最小生成树Prim算法)

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 connected, if and only if there is a road between A and B

hdu oj1102 Constructing Roads(最小生成树)

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

hdu Constructing Roads(最小生成树,kuskal算法)

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

HDU1102 Constructing Roads【Prim】

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

hdu 1102 Constructing Roads (最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14172    Accepted Submission(s): 5402 Problem Description There are N villa

HDU - 1102 - Constructing Roads (最小生成树--prim算法!!)

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

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