hdu1301 Jungle Roads 基础最小生成树

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4
 5 const int maxn = 10005;
 6 int n, m;
 7 int fa[28];
 8 struct node
 9 {
10     int x, y;
11     int cost;
12 }arr[maxn];
13
14 void init()
15 {
16     for (int i = 0; i <= maxn; i++)
17     {
18         fa[i] = i;
19     }
20 }
21
22 int find(int x)
23 {
24     if (x != fa[x])
25     {
26         return find(fa[x]);
27     }
28     else
29         return fa[x];
30 }
31
32 bool cmp(node a, node b)
33 {
34     return a.cost<b.cost;
35 }
36
37 int main()
38 {
39     char c1, c2;
40     int k, c;
41     while (cin >> n)
42     {
43         if (n == 0) break;
44         int j = 0;    //表示边的数量
45         init();
46         for (int i = 1; i<n; i++)
47         {
48             cin >> c1 >> k;
49             while (k--)
50             {
51                 cin >> c2 >> c;
52                 arr[j].x = c1 - ‘A‘;
53                 arr[j].y = c2 - ‘A‘;
54                 arr[j].cost = c;
55                 j++;
56             }
57         }
58         sort(arr, arr + j, cmp);    //排序
59         int ans = 0;
60         for (int i = 0; i<j; i++)
61         {
62             int fx, fy;
63             fx = find(arr[i].x);
64             fy = find(arr[i].y);
65             if (fx != fy)
66             {
67                 ans += arr[i].cost;
68                 fa[fy] = fx;
69             }
70         }
71         cout << ans << endl;
72     }
73     return 0;
74 }
时间: 2024-11-07 09:29:49

hdu1301 Jungle Roads 基础最小生成树的相关文章

HDU1301 Jungle Roads 【最小生成树Prim】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4268    Accepted Submission(s): 3120 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

HDU-1301 Jungle Roads(最小生成树[Prim])

Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 16   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description #include <iostream> #in

hdu1301&amp;poj1251 Jungle Roads(最小生成树之prim果题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1301 POJ: http://poj.org/problem?id=1251 一道prim算法的果题! 题目很长,这里就不贴题目了. 题意: 给你每个村庄之间的维护道路的费用,求最小的费用. 代码如下: #include <cstdio> #include <cstring> #include &

HDU1301 Jungle Roads【Prim】【最小生成树】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4591    Accepted Submission(s): 3371 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

POJ 1251 Jungle Roads (最小生成树)

POJ 1251 Jungle Roads Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road

POJ 1251 Jungle Roads(最小生成树)

题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接所有村子的最短路径 还是裸的最小生成树咯 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=30,M=1000; int par[N],n,m,ans; struct edge{int u

HDU - 1301 - Jungle Roads (最小生成树!!prim算法!!)

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4572    Accepted Submission(s): 3354 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

POJ1251 Jungle Roads 【最小生成树Prim】

Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some

HDU1301 Jungle Roads【Prim】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4554    Accepted Submission(s): 3341 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o