hdu1301Jungle Roads

http://acm.hdu.edu.cn/showproblem.php?pid=1301

最小生成树模板题

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 using namespace std;
 7 const int N=600;
 8 struct stu{
 9     int u;
10     int v;
11     int w;
12 }p[N];
13 int k,t;
14 int father[N];
15 int cmp(const void *a,const void *b)
16 {
17     return (*(struct stu*)a).w > (*(struct stu*)b).w ?1:-1;
18 }
19 int find(int x)
20 {
21     if(father[x]!=x)
22     father[x]=find(father[x]);
23     return father[x];
24 }
25 int make(int a,int b)
26 {
27     int h=0;
28     int f1=find(a);
29     int f2=find(b);
30     if(f1>f2)
31     {
32         father[f2]=f1;
33         h=1;
34     }
35     else if(f1<f2)
36     {
37         father[f1]=f2;
38         h=1;
39     }
40     return h;
41 }
42 int kruskal()
43 {
44     int h=0;
45     int s=0;
46     for(int i=0;i<k;i++)
47     {
48         if(make(p[i].u,p[i].v))
49         {
50             h++;
51             s+=p[i].w;
52         }
53         if(h==t-1)
54         return s;
55     }
56     return s;
57 }
58
59
60 int main()
61 {
62     //freopen("in.txt","r",stdin);
63     int n,m;
64     char a,c;
65     while(~scanf("%d",&t))
66     {
67         if(!t)
68         break;
69         for(int i=1;i<=t;i++)//刚开始t的位置我写的是常量N,就是过不去,我也是醉了!
70         father[i]=i;         //不知道咋回事
71         k=0;
72         getchar();
73
74         for(int i=1;i<t;i++)
75         {
76             scanf("%c %d",&a,&n);
77             for(int j=0;j<n;j++,k++)
78             {
79                 scanf(" %c %d",&c,&m);
80                 p[k].u=a-‘A‘+1;
81                 p[k].v=c-‘A‘+1;
82                 p[k].w=m;
83             }
84             getchar();
85         }
86         qsort(p,k,sizeof(struct stu),cmp);
87         //for(int i=0;i<k;i++)
88         //printf("%d %d %d\n",p[i].u,p[i].v,p[i].w);
89         printf("%d\n",kruskal());
90     }
91     return 0;
92 }
时间: 2024-10-03 13:27:24

hdu1301Jungle Roads的相关文章

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

Problem 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 network is too

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

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities)

【树形dp】Rebuilding Roads

[POJ1947]Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11934   Accepted: 5519 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake las

POJ——T2421 Constructing Roads

http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24132   Accepted: 10368 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can con

HDU1102--Constructing Roads(最小生成树)

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

Constructing Roads(最小生成树)

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

POJ1251 Jungle Roads

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24307   Accepted: 11430 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.

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路 题意: 数轴上n个互不覆盖的区间,问要用多少个长为L的线段覆盖.n≤10000 题解: 排序区间,然后从每个区间左端点开始铺木板,如果最后一块木板能够铺到下一个区间就铺,以此类推. 代码: 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #define maxn 10100 5 #define inc(i