题目链接:

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. Redis-跳跃表

    相当于Redis 中的 sorted set 跳跃表节点结构: typedef struct zskiplistNode { struct zskiplistNode *backward;  //后退 ...

  2. jquery特效:无缝向上循环滚动列表

    效果呈现 整个列表间隔设定的时间向上移动一个item的高度 html结构: <div class="slide-title"> <span>title1&l ...

  3. 使用匿名函数给setInterval()传递参数

    在使用JScript的时候,我们有时需要间隔的执行一个方法,比如用来产生网页UI动画特效啥的.这是我们常常会使用方法setInterval或setTimeout,但是由于这两个方法是由脚本宿主模拟出来 ...

  4. 【转载记录】Accessing Device Drivers from C#

    来源:http://www.drdobbs.com/cpp/accessing-device-drivers-from-c/184416423/   Device Drivers are writte ...

  5. Idea 快捷键大全【转】

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  6. String和StringBuffer有什么区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. java笔记--String类格式化当天日期转换符文档

    String类格式化当天日期 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877389.html "谢谢-- 转换符:% ...

  8. winfrom 实现窗体圆角

    在窗体中加入一下代码 #region 窗体圆角的实现 private void ComFrmBase_Resize(object sender, EventArgs e) { if (this.Win ...

  9. Python 词云 【中/英】小白简单入门教程

    1. 分析 构建词云需要具备: 原料即文章等内容 将内容进行分词 将分词后的内容利用构建词云的工具进行构建 保存成图片 2. 需要的主要模块 jieba 中文分词 wordcloud 构建词云 3. ...

  10. ORACLE 11GR2 安装时配置了域,后期删除

    因为用了一个安全平台.此平台居然不支持oracle中的服务吗有"."而这个点就是因为当时安装oracle录入了域.原来以为是修改服务名.百多了很多,最后发现就是删除域即可 感谢此文 ...