Jungle Roads_hdu_1301(prim算法)
Jungle Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3614 Accepted Submission(s): 2609

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.
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
30
import java.util.Scanner; public class Main{//prim算法,最优路径
private static int map[][];
private static boolean vis[];
private static int d[];
private static int MAXN=0x7ffffff;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(true){
int n=input.nextInt();
if(n==0)
break;
map=new int[n+1][n+1];
vis=new boolean[n+1];
d=new int[n+1];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=MAXN;
for(int k=1;k<n;++k){
String s=input.next();
int a=input.nextInt();
for(int i=0;i<a;i++){
String s1=input.next();
int b=input.nextInt();
int x=s.charAt(0)-'A'+1;
int y=s1.charAt(0)-'A'+1;
//System.out.println(x+"-----"+y);
map[x][y]=map[y][x]=b;
}
}
//prim算法
for(int i=1;i<=n;i++){
d[i]=map[1][i];
}
vis[1]=true;
d[1]=0;
int sum=0;
for(int i=2;i<=n;i++){
int min=MAXN;
int x=0,j;
for(j=1;j<=n;j++)
if(!vis[j]&&d[j]<min){
min=d[j];
x=j;
}
vis[x]=true;
sum+=min;
for(j=1;j<=n;j++){
if(!vis[j]&&d[j]>map[x][j]){
d[j]=map[x][j];
}
}
}
System.out.println(sum);
}
}
}
Jungle Roads_hdu_1301(prim算法)的更多相关文章
- 最小生成树问题------------Prim算法(TjuOj_1924_Jungle Roads)
遇到一道题,简单说就是找一个图的最小生成树,大概有两种常用的算法:Prim算法和Kruskal算法.这里先介绍Prim.随后贴出1924的算法实现代码. Prim算法 1.概览 普里姆算法(Prim算 ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 最小生成树のprim算法
Problem A Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...
- 最小生成树——prim算法
prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...
- 洛谷 P3366 【模板】最小生成树 prim算法思路 我自己的实现
网上有很多prim算法 用邻接矩阵 加什么lowcost数组 我觉得不靠谱 毕竟邻接矩阵本身就不是存图的好方法 所以自己写了一个邻接表(边信息表)版本的 注意我还是用了优先队列 每次新加入一个点 ...
- 最小生成树算法——prim算法
prim算法:从某一点开始,去遍历相邻的边,然后将权值最短的边加入集合,同时将新加入边集中的新点遍历相邻的边更新边值集合(边值集合用来找出新的最小权值边),注意每次更新都需将cost数组中的点对应的权 ...
- 贪心算法-最小生成树Kruskal算法和Prim算法
Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...
- Prim算法(三)之 Java详解
前面分别通过C和C++实现了普里姆,本文介绍普里姆的Java实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http:// ...
随机推荐
- jquery 查询IP归属地
<script src="http://c.csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type=&q ...
- gp数据库运维
最近需要将一份db2导出的历史数据入库gp集群,然后把每天的增量数据导出成txt文件和对应的log日志,再ftp传输给另外一台机器.其中陆续碰到一些坑,在此记录 历史文件数据清洗 列分隔符的选择 碰到 ...
- Rsync服务部署使用
rsync服务搭建过程(daemon模式) 配置服务 在/etc/rsyncd.conf文件中写入相应的配置: uid = root gid = root use chroot = no max co ...
- Highmaps网页图表教程之Highmaps第一个实例与图表构成
Highmaps网页图表教程之Highmaps第一个实例与图表构成 Highmaps第一个实例 下面我们来实现本教程的第一个Highmaps实例. [实例1-1:hellomap]下面来制作一个中国地 ...
- iOS WKWebview 网页开发适配指南
iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...
- C# NPOCO 轻量级ORM框架(入门)
目前公司使用这个框架,搜不到很详细的中文资料. 只有英文wiki,所以翻译学习一下. 因为博主也是低水平的,可能会有一些理解不到位的地方. 可能会有错误的地方,如果有园友发现可以指出. wiki地址: ...
- [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)
对于点集$A$,$B$,闵可夫斯基和$C=\{(x1+x2,y1+y2)|(x1,x2)\in A,(y1,y2)\in B\}$.由此可知,对于两个凸包$A$,$B$的闵可夫斯基和$C$满足,$C$ ...
- 删除JBOSS eap4.3下的jmx-console、web-console、ws-console、status服务
来源:http://iffiffj.iteye.com/blog/1404148 把下面代码保存为BAT文件,并放到JBOSS节点中运行. @echo off set HOME=%~dp0 set j ...
- 【BZOJ】1415: [Noi2005]聪聪和可可【期望】【最短路】【记忆化搜索】
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2335 Solved: 1373[Submit][Stat ...
- [LeetCode] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...