HDUOJ---1102Constructing Roads
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12173 Accepted Submission(s): 4627
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.
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.
0 990 692
990 0 179
692 179 0
1
1 2
最小生成树....
代码:
//最小生成树....
//@Gxjun coder
#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f ,v=;
int vis[v],lowc[v];
int sta[v][v];
int prim(int cost[][v],int n)
{
int i,j,p;
int minc,res=;
memset(vis , , sizeof(vis));
vis[]=;
for(i= ; i<n ;i++)
lowc[i]=cost[][i];
for(i=;i<n;i++)
{
minc=inf;
p=-;
for(j= ; j<n ;j++)
{
if(==vis[j] && minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
if(inf==minc) return -; //原图不连通
res+=minc ;
vis[p]=;
for(j=; j<n ;j++)
{
if(==vis[j] && lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
}
return res;
} int main()
{
int tol,res,i,j;
while(scanf("%d",&tol)!=EOF)
{
for(i=;i<tol;i++)
{
for(j=; j<tol;j++)
{
scanf("%d",&sta[i][j]);
}
}
scanf("%d",&res);
int num1,num2;
for(i=;i<res;i++)
{
scanf("%d%d",&num1,&num2);
sta[num1-][num2-]=sta[num2-][num1-]=;
}
printf("%d\n",prim(sta,tol));
}
return ;
}
HDUOJ---1102Constructing Roads的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- 杭电--1102--Constructing Roads--并查集
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
随机推荐
- eclipse无法启动
-startupplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar-showsplashorg.eclipse.platfo ...
- 使用StringBuilder或StringBuffer简单优化
使用StringBuilder或StringBuffer // join(["a", "b", "c"]) -> "a an ...
- Android系统file_contexts二进制与文本转换工具
#ifdef _WIN32 #define __USE_MINGW_ANSI_STDIO 1 #endif #include <stdio.h> #include <stdlib.h ...
- WampServer和phpStorm的用法
WampServer的安装 修改默认的浏览器和文本编辑器 phpStore创建一个PHP工程 在phpStore中运行我们的项目 配制一个PHP Script运行环境 配制一个PHP Web Page ...
- 合并JavaScript数组的N种方法
这是一篇简单的文章,关于JavaScript数组使用的一些技巧.我们将使用不同的方法结合/合并两个JS数组,以及讨论每个方法的优点/缺点. 让我们先考虑下面这情况: var a = [ 1, 2, 3 ...
- 【架构】SpringCloud 注册中心、负载均衡、熔断器、调用监控、API网关示例
示例代码: https://github.com/junneyang/springcloud-demo 参考资料: SpringCloud系列 Eureka 一句话概括下spring框架及spring ...
- spring data 自定义查询
spring data 自定义查询 https://www.cnblogs.com/airycode/p/6535635.html 在方法接口上面使用@Query
- simple-libfm-example-part1
原文:https://thierrysilbermann.wordpress.com/2015/02/11/simple-libfm-example-part1/ I often get email ...
- 用vs2013开发node.js的addon.
下载node.js的源代码. https://github.com/joyent/node 如果用svn下载,后面加上/trunk,以免把用不着的branches也下载下来,浪费时间. 安装V ...
- Office PPT中如何插入flash
1 在"视图"选项中找到工具栏,控件工具箱,最后一个其他工具中的shockwave flash object 2 当鼠标变成十字架之后随便画一个矩形,然后会出现一个白色的矩形中间十 ...