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> ...
随机推荐
- 移动开发基础和Dalvik VM
移动开发基础和Dalvik VM 1.基础 移动开发:是指以PDA.UMPC等便携终端为基础进行的开发工作. PDA:Person Digital Assistant 个人数码助理 UMPC:Ultr ...
- Go开发环境安装配置
访问下载地址:https://golang.org/dl/ 32位系统下载go1.8.1.linux-386.tar.gz,64位系统下载go1.8.1.linux-amd64.tar.gz, 假定你 ...
- English trip V1 - 1.How Do You Feel Now? Teacher:Lamb Key:形容词(Adjectives)
In this lesson you will learn to describe people, things, and feelings.在本课中,您将学习如何描述人,事和感受. STARTER ...
- 基础最短路(模板 spfa)
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- mysql判断表记录是否存在,不存在则插入新纪录
开始以为和SQL Server一样,使用not exists进行判断,结果不行: ) INSERT INTO vrv_paw_template(templateName,templateFileNam ...
- javassist和jdk动态代理
先来一个InvocationHandler示例,InvocationHandler类的作用是:对原始对象的方法做一个拦截. package com.zhang; import java.lang.re ...
- spring boot 学习(四)Druid连接池的使用配置
Druid介绍 Druid是一个JDBC组件,druid 是阿里开源在 github 上面的数据库连接池,它包括三部分: * DruidDriver 代理Driver,能够提供基于Filter-Cha ...
- idea列表
Idea列表
- 快速切题 sgu 111.Very simple problem 大数 开平方 难度:0 非java:1
111.Very simple problem time limit per test: 0.5 sec. memory limit per test: 4096 KB You are given n ...
- bzoj1648
题解: 简单灌水 然后统计一下 代码: #include<bits/stdc++.h> using namespace std; ; int ne[N],num,fi[N],n,k,m,x ...