HDU 1301Jungle Roads(最小生成树 prim,输入比较特殊)
题目链接:
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
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<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,输入比较特殊)的更多相关文章
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- hdu Jungle Roads(最小生成树)
Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...
- HDU 1301-Jungle Roads【Kruscal】模板题
题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- MST(最小生成树)——Prim算法——HDU 1879-继续畅通工程
Prim算法很好理解,特别是学完了迪杰斯特拉算法之后,更加能理解Prim的算法思想 和迪杰斯特拉算法差不多,由于最后要形成连通图,故任意指定一个点,作为初始点,遍历所有点,以当前最小权值的点(和迪杰斯 ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind
最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...
随机推荐
- CSS之Flex 布局
Flex 布局教程:语法篇 网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些 ...
- RabbitMq、ActiveMq、ZeroMq、kafka各个消息中间件之间的区别
MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求. 第一部分:RabbitMQ,ActiveMq, ...
- JavaScript中map函数和filter的简单举例
JavaScript的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似 1> filter是满足条件的留下,是对原数组的过滤:2> map则 ...
- 理解ASP.NET 5运行时命令:DNVM, DNX, 和DNU
ASP.NET 5 引入了一个新型的运行时,让我们可以现场交付模式组合式构建应用程序,而不依赖于宿主机上的.NET框架.这种新模式为我们提供了命令行工具(DNVM.DNX.DNU)用于管理我们的.ne ...
- centors7 elasticsearch6.3安装以及问题记录
1.安装elasticsearch . 安装系统:centors7 1.下载安装包 官网地址:https://www.elastic.co/downloads/past-releases 2.mac文 ...
- .net core系列之《从源码对Configuration的底层运行机制进行分析》
通过对Configuration源代码的分析从而来自定义一个配置数据源 1.用反编译工具来看看AddJsonFile()这个方法究竟干了什么,源代码如下: public static IConfigu ...
- MyCAT源码分析——分析环境部署
为了更好地了解mycat的原理,计划对mycat源码进行通读一遍,根据实际业务环境进行相关源码优化. 一.环境描述 操作系统:windows 10 x64 软件:jdk 1.7+ maven ...
- 使用FontAwesome
使用FontAwesome https://github.com/PrideChung/FontAwesomeKit 为什么要使用FontAwesome呢,其实,它的字体就是矢量图,无论是放大还是缩小 ...
- python使用SQLAlchemy对mysql操作
安装SQLAlchemy pip install sqlalchemy 在MySQL的test数据库中创建的user表,用SQLAlchemy来试试 数据库连接 第一步,导入SQLAlchemy,并初 ...
- Chapter 5 Order Inversion Pattern
5.1 Introdution The main focus of this chapter is to discuss the order inversion (OI) pattern, which ...