9852303 2013-12-18 11:47:01 Accepted 1301 0MS 264K 1117 B C++ 泽泽

Jungle Roads

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

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
 
prim算法:ps注意输入。

 #include<stdio.h>
#include<string.h>
int g[][];
int min,ans;
#define inf 0xffffff
void prim(int n)
{
int lowcost[],used[],closet[],i,j,k;
memset(used,,sizeof(used));
for(i=;i<=n;i++)
lowcost[i]=g[i][],
closet[i]=;
used[]=;
for(i=;i<n;i++)
{
j=;
min=inf;
for(k=;k<=n;k++)
if(lowcost[k]<min&&!used[k])
min=lowcost[k],j=k;
used[j]=;
ans+=g[j][closet[j]];
for(k=;k<=n;k++)
if(g[k][j]<lowcost[k]&&!used[k])
lowcost[k]=g[k][j],
closet[k]=j;
} }
int main()
{
int Q;
char s;
int x,i,j,k;
while(scanf("%d",&Q)!=EOF&&Q)
{
ans=;
for(i=;i<=Q;i++)
{
for(j=;j<=Q;j++)
{
g[i][j]=inf;
}
g[i][i]=;
}
getchar();
for(i=;i<=Q-;i++)
{
s=getchar();
int a=s-'A'+;
scanf("%d%*c",&k);
for(j=;j<=k;j++)
{
s=getchar();
int b=s-'A'+;
scanf("%d",&x);
getchar();
if(x<g[a][b])
g[a][b]=g[b][a]=x;
}
}
prim(Q);
printf("%d\n",ans);
}
return ;
}
9852303 2013-12-18 11:47:01 Accepted 1301 0MS 264K 1117 B C++ 泽泽
 

HDOJ 1301的更多相关文章

  1. HDOJ 1301 Jungle Roads

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...

  2. HDOJ 1301最小生成树的Kruskal算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 将结点的字符信息处理成点信息即可,代码如下: #include<bits/stdc++.h ...

  3. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  4. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

随机推荐

  1. Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录

    第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...

  2. WCF调用时提示错误 "已尝试创建到达不支持 .Net 框架的服务的通道。可能遇到 HTTP 终结点"

    一个以前运行的很正常的项目,某天突然无法连接WCF构建的后台.使用WCFTestClient连接到服务是正常的,但是调用服务中的方式时就报出了以下错误: 已尝试创建到达不支持 .Net 框架的服务的通 ...

  3. 小菜鸟学 Spring-Dependency injection(二)

    注入方式一:set注入 <bean id="exampleBean" class="examples.ExampleBean"> <!-- s ...

  4. Mathematical operation

    (1)Using let let result=2+1 let result=2-1 let result=2*1 let result=2/1(2) Using bracket echo $(($p ...

  5. Git环境的搭建及使用

    管理工具 1. Git环境的搭建 a.下载Git installer,地址:http://git-scm.com/downloads a1.参考文档地址:http://www.open-open.co ...

  6. Rootkit Hunter Sourcecode Learning

    目录 . Rootkit Hunter Introduce() . Source Code Frame() . do_system_check_initialisation() . do_system ...

  7. ActivityInfo taskAffinity

    通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...

  8. C#实现自动升级(附源码)

    http://blog.csdn.net/zhuweisky/article/details/50439386 OAUS

  9. shell中条件判断if中的-z到-d的意思

    shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 ...

  10. WinForm AutoComplete 输入提示、自动补全

    一.前言 又临近春节,作为屌丝的我,又要为车票发愁了.记得去年出现了各种12306的插件,最近不忙,于是也着手自己写个抢票插件,当是熟悉一下WinForm吧.小软件还在开发中,待完善后,也写篇博客与大 ...