hdu oj1102 Constructing Roads(最小生成树)
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13995 Accepted Submission(s): 5324
C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
3 0 990 692 990 0 179 692 179 0 1 1 2
179
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int maxn=101;
- const int Max=0x3f3f3f3f;
- int map[maxn][maxn],low[maxn],visit[maxn];
- int n;
- int prim()
- {
- int i,j,pos,min,mst=0;
- memset(visit,0,sizeof(visit));
- pos=1;
- visit[1]=1;
- for(i=1;i<=n;i++)
- low[i]=map[pos][i];
- for(i=1;i<n;i++)
- {
- min=Max;
- for(j=1;j<=n;j++)
- {
- if(!visit[j] && min>low[j])
- {
- min=low[j];
- pos=j;
- }
- }
- mst+=min;
- if(mst>=Max) break;//说明这个图不连通
- visit[pos]=j;
- for(j=1;j<=n;j++)
- {
- if(!visit[j] && low[j]>map[pos][j])//更新low数组
- low[j]=map[pos][j];
- }
- }
- return mst;
- }
- int main()
- {
- int q,i,j;
- int a,b;
- while(scanf("%d",&n)!=EOF)
- {
- memset(map,Max,sizeof(map));
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- scanf("%d",&map[i][j]);
- scanf("%d",&q);
- while(q--)
- {
- scanf("%d%d",&a,&b);
- map[a][b]=map[b][a]=0;
- }
- printf("%d\n",prim());
- }
- return 0;
- }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
hdu oj1102 Constructing Roads(最小生成树)的更多相关文章
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
随机推荐
- Android UI法宝发展Angrytools
最近很多人问我,个人App开发商如何设计UI. 其实这是个人开发者最头疼,谁在搞技术,真的不能做的一切.不可能花大量的时间去切图,去做原型设计,去做美工. 当然,尽管我们设计不出那么复杂,静止的UI. ...
- Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)
Phylogenetic Trees Inherited Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 480 Accepted ...
- .net与Java的WebService互调
本文记录一下.net与Java是如何进行Web Service的互相调用的. 1.准备工作 MyEclipse 10 JDK 1.6.0_13 Visual Studio 2012 .net fram ...
- Android 应用程序窗口显示状态操作(requestWindowFeature()的应用)
我们在开发程序是常常会须要软件全屏显示.自己定义标题(使用button等控件)和其它的需求,今天这一讲就是怎样控制Android应用程序的窗口显示. 首先介绍一个重要方法那就是requestWi ...
- SQL Server BCP使用小结
原文:SQL Server BCP使用小结 用法: bcp {dbtable ) );GO--输出XML格式化文件--说明一下:-t","是指定字段分隔符,稍后我们会讲到exe ...
- CloudFoundry.yml修订
--- name: CFRELEASE02 director_uuid: fdd46e30-f2c5-41dc-9662-0976fdac5716 releases: - name: cf versi ...
- S关于使用QL声明 找出同时满足多个tag拍摄条件设置算法
表结构 Tag Table:{tag_id, tag_name} #标签表 News Table:{news_id, title,......} #新闻列表 NewsTags Table:{tag ...
- 使用javascript实现html文字不可选
如何使用js让html该文本是不可选定它?首先想到的是用css选择实现,如下面: -webkit-touch-callout: none; -webkit-user-select: none; -kh ...
- Android定调的发展
首先,介绍一下Android系统支持的铃声格式. 有下面几种: 64赫兹Midi,AAC.AAC+.AMR.WAV.MP3.Real Audio.WMA.OGG等格式. 将音频文件设置成铃声非常eas ...
- CURL重试发送请求
$url1 = 'http://mk.2000tuan.com/coupon4/apiv2/getMcoupon.php; function curlGet($url) { $handle = cur ...