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. grunt安装

    随着node的流行,各种后台的技术应用到前端,依赖注入.自动化测试.构建等等. 本篇就介绍下如何使用Grunt进行构建. grunt安装 由于grunt依赖于nodejs,因此需要先安装nodejs. ...

  2. please tell me the error about java Graphics

    this is the code: public class paint extends JFrame{ public paint(){ setLayout(new FlowLayout()); se ...

  3. json_decode详解

    json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON 格式的字符串进行编码.    json_decode的语法规则:json_decode ( string $j ...

  4. 每天一个linux命令(25):df 命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  5. HP 电脑装 纯净版的win7

    新买的 HP 电脑,自带 Win10 的操作系统,今天把它改成 装win7 系统 在安装的过程中遇到的问题 1.数字证书错误.您安装的操作系统来源不明之类的错误,具体没有记下来 2.磁盘的格式不是NT ...

  6. ACM算法总结及刷题参考

    参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,p ...

  7. 【bzoj1202】 HNOI2005—狡猾的商人

    http://www.lydsy.com/JudgeOnline/problem.php?id=1202 (题目链接) 题意 给出m段区间和,判断是否存在某段区间与之前读入的区间相矛盾. Soluti ...

  8. DEDECMS数据库执行原理、CMS代码层SQL注入防御思路

    我们在上一篇文章中学习了DEDECMS的模板标签.模板解析原理,以及通过对模板核心类的Hook Patch来对模板的解析流量的攻击模式检测,达到修复模板类代码执行漏洞的目的 http://www.cn ...

  9. C#文件复制功能

    目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码: sing System; using System.Collections.Generic; using System.C ...

  10. 慎用 Enum.GetHashCode()

    公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...