Jungle Roads

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

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
 
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算法)的更多相关文章

  1. 最小生成树问题------------Prim算法(TjuOj_1924_Jungle Roads)

    遇到一道题,简单说就是找一个图的最小生成树,大概有两种常用的算法:Prim算法和Kruskal算法.这里先介绍Prim.随后贴出1924的算法实现代码. Prim算法 1.概览 普里姆算法(Prim算 ...

  2. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  3. 最小生成树のprim算法

    Problem A Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

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

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

  5. 最小生成树——prim算法

    prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...

  6. 洛谷 P3366 【模板】最小生成树 prim算法思路 我自己的实现

    网上有很多prim算法  用邻接矩阵 加什么lowcost数组 我觉得不靠谱 毕竟邻接矩阵本身就不是存图的好方法 所以自己写了一个邻接表(边信息表)版本的  注意我还是用了优先队列  每次新加入一个点 ...

  7. 最小生成树算法——prim算法

    prim算法:从某一点开始,去遍历相邻的边,然后将权值最短的边加入集合,同时将新加入边集中的新点遍历相邻的边更新边值集合(边值集合用来找出新的最小权值边),注意每次更新都需将cost数组中的点对应的权 ...

  8. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  9. Prim算法(三)之 Java详解

    前面分别通过C和C++实现了普里姆,本文介绍普里姆的Java实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http:// ...

随机推荐

  1. PHP接入支付宝支付

    创建应用 使用支付宝账号登录开放平台创建应用,应用创建成功之后可以得到APPID等相关信息 接着需要设置RSA密钥,可以使用蚂蚁金服开放平台提供的生成工具,生成完密钥需在开放平台中填写. 代码接入 引 ...

  2. 【转】JQuery Validate使用总结1

    一.导入js库 <script src="../js/jquery.js" type="text/javascript"></script&g ...

  3. Android 中的广播(Broadcast)

    Android 广播(broadcast) 饮水思源 本文章内容学习和总结自 郭霖大神:<Android第一行代码> Overview 就像我们的学校里的喇叭一样,是用来通知的.而Andr ...

  4. C# CuttingEdge.Conditions 验证帮助类库 文档翻译

    项目主页: https://archive.codeplex.com/?p=conditions 作者博客关于项目的文档(翻译原文): https://www.cuttingedge.it/blogs ...

  5. luoguP3979 遥远的国度 树链剖分

    \(1, 2\)操作没什么好说的 对于\(3\)操作,分三种情况讨论下 \(id = rt\)的情况下,查整棵树的最小值即可 如果\(rt\)在\(1\)号点为根的情况下不在\(id\)的子树中,那么 ...

  6. SPOJ GSS

    GSS1 题目大意:给出一个数列,多次询问区间最长连续子段和 题解:线段树维护区间最长连续子段和gss,区间从最左元素开始的最长连续子段和lgss 区间以最右元素为结尾的最长连续子段和rgss以及区间 ...

  7. [POI2015]Pieczęć

    [POI2015]Pieczęć 题目大意: 一张\(n\times m(n,m\le1000)\)的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色. 你有一个\(a\times b(a,b\l ...

  8. CentOS的利手:“Screen”一个可以在多个进程之间多路复用一个物理终端的窗口管理器

    你是不是经常需要远程登录到Linux服务器?你是不是经常为一些长时间运行的任务头疼?还在用 nohup 吗?那 么来看看 screen 吧,它会给你一个惊喜! 你是不是经常需要 SSH 或者 tele ...

  9. 洛谷P3119 USACO15JAN 草鉴定

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  10. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...