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. jQuery应用之(一)使用jQuery选择器(荐)

    如上文(地址)jQuery预先的javascript的编程,提供了计划所有css3下的标准选择器,开发者可以利用这些选择器轻松选择各种元素,供javascript使用. 重要的是jQuery对这些选择 ...

  2. beta分工

    队伍CleanCode 031302505   黄晓辉(21%) 031302223   翁瀚帅(19%) 031302511   林培兴(22%) 031302632   张衍坤(19%) 0313 ...

  3. poj 3669 线段树成段更新+区间合并

    添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...

  4. POJ3749 破译密码

    Description 据说最早的密码来自于罗马的凯撒大帝.消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A都分别替换成字母F).而你要获得消息 ...

  5. 洛谷P1363 幻想迷宫

    题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...

  6. 以一个权限系统来告别WebForm —(一)项目整休架构设计与数据库设计

    在本节我想与大家与分享一下,我所将要做的权限系统的架构和数据库的表的设计.请各位大神们对我项目中设计的不足之处进行指导,让我得以更好的写完它,留给需要它的人. 我的项目架构如下图所示: 如上图所示,在 ...

  7. ubuntu安装spark

    1.先得准备环境,需要JAVA环境,还有Python环境(默认会有) JAVA下载JDK之后配置JAVA环境变量 export JAVA_HOME=/opt/jdk1..0_45 export JRE ...

  8. ORACLE RAC集群的体系结构

    RAC是一个完整的集群应用环境,它不仅实现了集群的功能,而且提供了运行在集群之上的应用程序,即Oracle数据库.无论与普通的集群相比,还是与普通的Oracle数据库相比,RAC都有一些独特之处. R ...

  9. laravel框架中的session问题

    这两天一直在鼓捣服务器,配置环境,在搭建laravel的过程之中,发现了laravel中的session的一些问题,这里总结一下: (1):我在服务器上搭建了多个sever,为了测试学习,分别使用不同 ...

  10. AppStore占坑注意事项

    AppStore占坑注意事项 我们会提前在AppStore(iTunesConnect)里注册一些应用名称,以满足未来业务需要和防止恶意注册,其中有一些需要注意的事情,整理如下: 倒计时180天 为了 ...