HDOJ 1301
| 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
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
#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的更多相关文章
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
- HDOJ 1301最小生成树的Kruskal算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 将结点的字符信息处理成点信息即可,代码如下: #include<bits/stdc++.h ...
- 杭电hdoj题目分类
HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
- HDOJ 题目分类
HDOJ 题目分类 /* * 一:简单题 */ 1000: 入门用:1001: 用高斯求和公式要防溢出1004:1012:1013: 对9取余好了1017:1021:1027: ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
随机推荐
- Git代码管理心得
一.概述: 这次按照要求进行了看似复杂,实则非常复杂并且麻烦(网上教程众多且啰嗦)的对git使用的学习,从星期六晚18:48我准备这次作业开始,直到了晚上22:44才结束电脑上的操作···(导致这篇随 ...
- jquery 在 table 中修改某行值
修改 table 中某行的的方法步骤如下: 1.选择要修改的行,事件触发,比如我的 双击某行时修改 2.将要修改的行,替换为input,原先的列中的值,需要放到对应的input中作为默认值 3.修改完 ...
- 【POJ 3020】Antenna Placement(二分图匹配)
相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...
- POJ3579 Median
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- php版本的discuzX3.2部署的问题收集
1.登陆后台老是自动退出是怎么回事? 解决方法:用ftp上線下載文件下在跟目錄/config/config_global.php把$_config['admincp']['checkip'] = 1 ...
- 洛谷P1661 扩散
题目描述 一个点每过一个单位时间就会向四个方向扩散一个距离,如图. 两个点a.b连通,记作e(a,b),当且仅当a.b的扩散区域有公共部分.连通块的定义是块内的任意两个点u.v都必定存在路径e(u,a ...
- javax/faces/webapp/FacesServlet
严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component ...
- urllib2.URLError: <urlopen error [Errno 10061] >
今天来运行以前的python脚本,结果报这个错:urllib2.URLError: <urlopen error [Errno 10061] > 原来是因为 解决方法:打开IE浏览器,依次 ...
- POJ2049Finding Nemo(bfs + 构图)
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8456 Accepted: 1975 Desc ...
- easyUI框架之学习1--框架
<!DOCTYPE html><html> <head > <link href="~/Scripts/easyUI/themes/default/ ...