https://vjudge.net/problem/POJ-1251


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 expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

Sample Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

Sample Output

216
30
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define maxn 30
#define ms(x,n) memset(x,n,sizeof x);
const int inf=0x3f3f3f3f;
using namespace std;
int parent[maxn],ranks[maxn];
int V,E;
int finds(int x)
{
if(x!=parent[x])
return parent[x]=finds(parent[x]);
}
void init(int n)
{
for(int i=;i<n;i++)
parent[i]=i,ranks[i]=;
}
void unite(int x,int y)
{
x=finds(x),y=finds(y);
if(x==y)return;
if(ranks[x]>ranks[y])
parent[y]=x;
else
{
parent[x]=y;
if(ranks[x]==ranks[y])
ranks[y]++;
}
}
struct node
{
int u,v,w;
node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}
node(){u=,v=,w=inf;}
}edge[maxn*maxn];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int kruskal()
{
sort(edge,edge+E,cmp);
init(V);
int ans=;
for(int i=;i<E;i++)
{
node a=edge[i];
if(finds(a.u)!=finds(a.v))
{unite(a.u,a.v);
ans+=a.w;
}
}
return ans;
}
int main()
{
int k,w;
char u,v;
while(~scanf("%d",&V),V)
{
E=;
for(int i=;i<maxn;i++)
edge[i]=node();
for(int i=;i<V-;i++)
{
cin>>u>>k;
while(k--)
{
cin>>v>>w;
edge[E++]=node(u-'A',v-'A',w);
}
}
cout<<kruskal()<<endl;
}
return ;
}

题目大意:

给定相互联通的边和权值,求最小生成树。

思路:

因为是稀疏图,推荐用Kruskal。

注意点:

在这道题中学会了给结构体的对象设初值node(){u=0,v=0,w=inf;}和赋值node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}。

POJ1251(Kruskal水题)的更多相关文章

  1. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  2. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  4. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  5. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  6. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  7. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  8. 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题

    B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大 ...

  9. ACM水题

    ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: ...

随机推荐

  1. 【Java并发编程】6、volatile关键字解析&内存模型&并发编程中三概念

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  2. canvas-star5.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. JS校验身份证号的合法性

    前端表单中有身份证号的校验,下边是用JS来校验身份证号的合法性. 中国居民身份证号码编码规则 第一.二位表示省(自治区.直辖市.特别行政区). 第三.四位表示市(地级市.自治州.盟及国家直辖市所属市辖 ...

  4. What I am concerned about

    redux: https://www.cnblogs.com/XieJunBao/p/9232341.html vuex: https://juejin.im/post/5a8eb24e6fb9a06 ...

  5. 惊闻企业Web应用生成平台 活字格 V4.0 免费了,不单可视化设计器免费,服务器也免费!

    官网消息: 针对活字格开发者,新版本完全免费!您可下载活字格 Web 应用生成平台 V4.0 Updated 1,方便的创建各类 Web 应用系统,任意部署,永不过期. 我之前学习过活字格,也曾经向用 ...

  6. Linux高可靠技术

    1.进程挂死时,有后台监控程序检测重新拉起. 2.进程占用系统资源超过ulimit限定的资源时,会被ulimit杀死,同时配合后台监控程序,重新拉起进程,实现进程可靠性. 3.Linux系统的高可靠性 ...

  7. 创建Android Apps的30个经验教训

    这个世界上有两种人-从经验教训中学习的人以及听从别人建议的人.这里是我一路走来学到的一些东西,分享给大家: 在添加任何第三方party之前,请三思:这真的是一个成熟的项目吗? 如果一个东西用户看不到, ...

  8. Python-初识模块

    #系统自带的模块 import sys print(sys.path)#打印环境变量 print(sys.argv)#打印绝对路径 import os #cmd_res = os.system(&qu ...

  9. tkinter中lable标签控件(二)

    lable控件 对于tkinter来说,学起来很简单,只要设置好相应的参数即可出结果,所以不用刻意去记住这些参数.学习一遍后理解每个参数的作用是什么即可. 当下次用到的时候来笔记上看一下就行. 内容很 ...

  10. python第一百零五天 ---Django 基础 路由系统 URL 模板语言 ORM 操作

    一 路由系统 URL 1 url(r'^index/',views.index) url(r'^home/', views.Home.as_view()) 2 url(r'^detail-(\d+). ...