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> ...
随机推荐
- 网站链接facebook 拿新的post
$http({ method: "GET", url: "https://graph.facebook.com/oauth/access_token?client_id= ...
- C# Random循环生成随机数重复问题解决方案
C# Random循环生成随机数重复问题解决方案1.当我们通过Random生成随机数时,习惯的写法如下: int a=new Random().Next(0,100); 然后生成一个数据数没有任何问题 ...
- m_Orchestrate learning system---三十二、数据库字段判断为空时容易出现问题,如何从根本上解决这个问题
m_Orchestrate learning system---三十二.数据库字段判断为空时容易出现问题,如何从根本上解决这个问题 一.总结 一句话总结:字段禁止为空,设置默认值0即可 禁止 空 默认 ...
- Silverlight自定义控件系列 – TreeView (4) 缩进
接下来是缩进,没有缩进的Tree怎么看都不顺眼. 首先,定义节点深度Depth(注:回叫方法暂没有代码,以后要用到): 1: /// <summary> 2: /// Using a De ...
- 多线程(JDK1.5的新特性互斥锁)
多线程(JDK1.5的新特性互斥锁)(掌握)1.同步·使用ReentrantLock类的lock()和unlock()方法进行同步2.通信·使用ReentrantLock类的newCondition( ...
- RMQ板子
对于RMQ这种静态最值询问, 用线段树的话查询过慢, 一般用ST表预处理后O(1)查询, 下以最大值查询为例, 这里假定$n$不超过5e5 void init() { Log[0] = -1; REP ...
- drawImage
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- darktrace 亮点是使用的无监督学习(贝叶斯网络、聚类、递归贝叶斯估计)发现未知威胁——使用无人监督 机器学习反而允许系统发现罕见的和以前看不见的威胁,这些威胁本身并不依赖 不完善的训练数据集。 学习正常数据,发现异常!
先说说他们的产品:企业免疫系统(基于异常发现来识别威胁) 可以看到是面向企业内部安全的! 优点整个网络拓扑的三维可视化企业威胁级别的实时全局概述智能地聚类异常泛频谱观测 - 高阶网络拓扑;特定群集,子 ...
- custom usb-seriel udev relus for compatible usb-seriel devices using kermit
custom usb-seriel udev relus for compatible usb-seriel devices add-pl2303.rules: ACTION== "add& ...
- php抓取文章内容分析
preg_match_all — 执行一个全局正则表达式匹配 int preg_match_all ( string pattern, string subject, array matches [, ...