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 ...
随机推荐
- centos6.5 64位 openvpn安装配置(转)
查看系统版本cat /etc/redhat-releaseCentOS release 6.5 (Final) 查看内核和cpu架构uname -rm2.6.32-431.el6.x86_64 x86 ...
- [MySQL]-->询5
几天之内解决在新年发行一个同事的生日过程
前言: 遇到朋友提问,例如以下:SELECT * FROM ali_users WHERE DATEDIFF(CAST(CONCAT(DATE_FORMAT(NOW(),'%y'),D ...
- lsblk请参阅块设备
lsblk可以查看分区和挂载的磁盘使用情况 lsblk全部的參数 -a, --all 显示全部设备 -b, --bytes 以bytes方式显示设备大小 - ...
- dom 规划(html和xml)
html dom与xml dom关联: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...
- hdu 5076 最小割灵活运用
这意味着更复杂的问题,关键的事实被抽象出来:每个点,能够赋予既有的值(挑两个一.需要选择,设定ai,bi). 寻找所有和最大.有条件:如果两个点同时满足: 1,:二进制只是有一个不同之处. 2:中的 ...
- linux笔记本上安装了双显卡驱动(intel+nvidia)
为了提高linux图形性能并降低功耗,独特的文章. 我用的系统Fedora 20 Xfce x64,在安装驱动程序,以确保系统是最新的版本号. 最好安装gcc.kernel-devel和其他包.己主动 ...
- DirectX 11游戏编程学习笔记2: 文章1章Vector Algebra(向量代数)
本文由哈里_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com 注:我给的电子版是700多页.而实体书是800多页,所以我在提到相关概念的时候.会使用章节号而 ...
- Git 少用 Pull 多用 Fetch 和 Merge(转)
英文原文:git: fetch and merge, don’t pull This is too long and rambling, but to steal a joke from Mark T ...
- JavaEE(8) - 本地和远程调用的有状态以及无状态Session EJB
1. 使用NetBeans开发Session Bean #1. 创建项目:File-->New Project-->Java EE-->EJB Module #2. 在项目中创建Se ...
- Spring3.0学习笔记文档的官方网站(六)--3.4.1
3.4 依靠 3.4.1 依赖注入 依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI 参数是引进一个对象的.和缺乏父母之前-子类关系: ...