hdu1301 Jungle Roads 最小生成树
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.
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.
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.
其实就是最小生成树裸题
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int fa[],n; struct seg{
int a,b,l;
}s[]; bool cmp(seg a,seg b){
return a.l<b.l;
} void init(){
int i;for(i=;i<=n;i++)fa[i]=i;
} int find(int x){
if(fa[x]==x)return x;
fa[x]=find(fa[x]);
return fa[x];
} void unio(int x,int y){
int dx=find(x),dy=find(y);
if(dx!=dy)fa[dx]=dy;
} int main(){
while(scanf("%d",&n)!=EOF&&n!=){
char a[],b[];
int i,t,c=;
memset(s,,sizeof(s));
for(i=;i<n;i++){
scanf("%s%d",a,&t);
int v;
for(int j=;j<=t;j++){
scanf("%s%d",b,&v);
s[++c].a=a[]-'A'+;
s[c].b=b[]-'A'+;
s[c].l=v;
}
}
init();
sort(s+,s+c+,cmp);
int ans=;t=;
for(i=;i<=c;i++){
if(find(s[i].a)==find(s[i].b))continue;
ans+=s[i].l;
unio(s[i].a,s[i].b);
t++;
if(t==n-)break;
}
printf("%d\n",ans);
}
return ;
}
hdu1301 Jungle Roads 最小生成树的更多相关文章
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- HDU1301 Jungle Roads
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- hdu1301 Jungle Roads 基础最小生成树
#include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- hdu Jungle Roads(最小生成树)
Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...
- HDU1301 Jungle Roads(Kruskal)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- hdu1301 Jungle Roads (Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 依旧Prim............不多说了 #include<iostream> ...
- hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)
普里姆求解: #include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
随机推荐
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
- union和union all比较说明
执行sql语句:select '1' union select '3' union select '2' union select '1' 得到的结果集如下: 执行sql语句如下: select ' ...
- [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...
Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename... [可能没有默认的字体] 例: //putenv('G ...
- codeforces 555c// Case of Chocolate// Codeforces Round #310(Div. 1)
题意:直角边为n的网格巧克力,一格为一块,选择斜边上一点,从左或上吃,直到吃到空气,称为一次操作.给出几个操作,问各能吃几块.如果x是当前要吃的横坐标,在已经吃过的中找x1>=x的第一个x1,即 ...
- 『Re』知识工程作业_主体识别
作业要求 环境路径 类似于这样的,一共50篇文档, 均为中文文档,是法院判决书的合集. 程序 程序如下,我完全使用正则表达式来实现功能, import re import glob import co ...
- python-day34--并发编程之多线程
理论部分 一.什么是线程: 1.线程:一条流水线的工作过程 2.一个进程里至少有一个线程,这个线程叫主线程 进程里真正干活的就是线程 3.进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资 ...
- store procedure
store procedure: _________________________________________________ set ANSI_NULLS ON set QUOTED_IDEN ...
- 查询(sqlSuger)
查某一天的数据记录的条数 DateTime date1 = Convert.ToDateTime(DateTime.Now.ToShortDateString()); DateTime date2 = ...
- java keytool详解
Keytool 是一个Java 数据证书的管理工具 ,Keytool 将密钥(key)和证书(certificates)存在一个称为keystore的文件中. 在keystore里,包含两种数据:(1 ...
- 关于PermGen space内存溢出错误解决方法
1.参考: http://blog.csdn.net/fox009/article/details/5633007 http://hi.baidu.com/like_dark/blog/item/19 ...