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 ...
随机推荐
- 简单QT应用了可实现手动布局QT应用
新建QT项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...
- Objective-C路成魔【2-Objective-C 规划】
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 编译执行O ...
- Fizz-Buzz-Whizz
Fizz-Buzz-Whizz ThoughtWorks测试 问题描述: 1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3.5.7. 2. 让所有学生拍成一队,然后按顺序报数. 3. 学生报 ...
- Canvas 生成base64的PNG图片快照,So Amazing!!!
function canvasSupport(){ return Modernizr.canvas;}function callCanvasApps(){ var canvasOne=doc ...
- HDU 1711 Number Sequence(kmp)
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- 设计模式入门之装饰器模式Decorator
//装饰模式定义:动态地给一个对象加入一些额外的职责. //就添加功能来说.装饰模式比生成子类更为灵活 //这也提现了面向对象设计中的一条基本原则,即:尽量使用对象组合,而不是对象继承 //Compo ...
- SCM文章4教训:定时器共阴极LED动态显示屏
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva290ZWlfODhfbHVsdWNfNjY=/font/5a6L5L2T/fontsize/400/fil ...
- jQuery.reveal弹出层
jQuery.reveal弹出层使用 最近用到弹出层,还得自定义UI,原本用的artDialog太庞大,不合适了,于是就找到了这个东西,又小又好用,基础的弹出遮罩都有了,想要什么还不是Coder自己说 ...
- SQLServer 存储过程嵌套事务处理
原文:SQLServer 存储过程嵌套事务处理 某个存储过程可能被单独调用,也可能由其他存储过程嵌套调用,则可能会发生嵌套事务的情形. 下面是一种解决存储过程嵌套调用的通用代码,在不能确定存储过程是否 ...
- 详细说明C++笔试题,调查超载、盖、多态
C++可见版本,他说,这本书是采访的主题,调查超载.盖.多态性等概念,比较有代表性的.今天上午,远程辅导 Yan Wang 学生们学习 Qt 时还觉得这个话题,假设你能正确地理解这一主题,注意对于 C ...