题目链接:

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

Jungle Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9444    Accepted Submission(s): 6924

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 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.

 
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
 
Source
分析:
注意输入,模板题,很水
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000
#define max_v 30
int g[max_v][max_v];//g[i][j] 表示i点到j点的距离
int n,sum;
void init()
{
for(int i=; i<n; i++)
for(int j=; j<n; j++)
g[i][j]=INF;
}
void prim()
{
// int close[n];//记录不在s中的点在s中的最近邻接点
int lowcost[n];//记录不在s中的点到s的最短距离,即到最近邻接点的权值
int used[n];//点在s中为1,否则为0
for(int i=; i<n; i++)
{
//初始化,s中只有一个点(0)
lowcost[i]=g[][i];//获取其他点到0点的距离,不相邻的点距离无穷大
// close[i]=0;//初始化所有点的最近邻接点都为0点
used[i]=;//初始化所有点都没有被访问过
}
used[]=;
for(int i=; i<n; i++)
{
//找点
int j=;
for(int k=; k<n; k++) //找到没有用过的且到s距离最小的点
{
if(!used[k]&&lowcost[k]<lowcost[j])
j=k;
}
// printf("%d %d %d\n",close[j]+1,j+1,lowcost[j]);
sum+=lowcost[j];
used[j]=;//j点加入到s中
//松弛
for(int k=; k<n; k++)
{
if(!used[k]&&g[j][k]<lowcost[k])
{
lowcost[k]=g[j][k];
// close[k]=j;
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
sum=;
init();
getchar();
for(int i=;i<n-;i++)
{
char c;
scanf("%c",&c);
int m;
scanf("%d",&m);
for(int j=;j<m;j++)
{
getchar();
char d;
scanf("%c",&d);
int x;
scanf("%d",&x);
g[c-'A'][d-'A']=x;
g[d-'A'][c-'A']=x;
}
getchar();
}
prim();
printf("%d\n",sum);
}
return ;
}

HDU 1301Jungle Roads(最小生成树 prim,输入比较特殊)的更多相关文章

  1. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  2. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  3. hdu Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  4. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...

  5. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  6. MST(最小生成树)——Prim算法——HDU 1879-继续畅通工程

    Prim算法很好理解,特别是学完了迪杰斯特拉算法之后,更加能理解Prim的算法思想 和迪杰斯特拉算法差不多,由于最后要形成连通图,故任意指定一个点,作为初始点,遍历所有点,以当前最小权值的点(和迪杰斯 ...

  7. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  8. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  9. 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind

    最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...

随机推荐

  1. docker启动容器报错:IPv4 forwarding is disabled. Networking will not work

    报这个错误会导致宿主机以外的pc 访问不了容器 按照网上的解决办法: 在/usr/lib/sysctl.d/00-system.conf文件后加net.ipv4.ip_forward=1 即可 然后重 ...

  2. 解决:启动项目报错 java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

    前言:项目在 spring-mvc.xml 文件中配置了上传文件拦截,结果启动报错 java.lang.NoClassDefFoundError: org/apache/commons/fileupl ...

  3. 从接口取到的JSON数据如何导入进本地SQL数据库

    新手学习,求大神指点. 首先:在数据库建立表--设计字段(最好和接口说明文档里面的一致) 第一步:获取接口中的字符串:这里获取的是有转义字符的字符串 HttpWebRequest request = ...

  4. Android 二次打包(封装)AAR实用指南

    前言 上次文章Android SDK开发与使用的那些事儿说到如何在aar里集成导入的aar,也就是二次封装aar的问题,一带而过,难免不过瘾.在封装这条路上也遇到了不少坑,现在将此方法详细记录下来. ...

  5. PHP两种实现无级递归分类的方法

    /** * 无级递归分类 TP框架 * @param int $assortPid 要查询分类的父级id * @param mixed $tag 上下级分类之间的分隔符 * @return strin ...

  6. Spring事务管理入门与进阶

    事务管理 一个数据库事务是一个单一的工作单元操作序列.这些操作应该要么完整地执行,要么完全不执行.事务管理是一个重要组成部分,事务管理可以确保数据完整性和一致性.事务具有以下四个关键特性分别是 ACI ...

  7. C语言中关键词static的用法与作用域

    一.面向过程设计中的static 转载:http://blog.csdn.net/celerylxq/article/details/6160499 1.静态全局变量 在全局变量前,加上关键字stat ...

  8. 解决Spring框架下中文乱码的问题

    在使用了Spring框架下回发现很多表单交互的地方会发生乱码,而且写到数据库中也是乱码,这其实还是字符编码的问题,在我们还在用自己写的servlet的时候,直接在request和response加上字 ...

  9. 在 O(1) 时间删除链表结点(C 和 Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 给定单向链表的头指针和一个结点指针,定义一个函数在 O(1) ...

  10. pmp心得

    我报名比较晚,等缴费最后期限,才缴费,下定决心,开始正式的备考. 我的工作比较忙,备考时间特比较短,从拿到书到考试只有二个月了,心理慌慌的,期间还有一门其他的考试,在5月底,实际时间只能有20来天. ...