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 ...
随机推荐
- Vim global命令和重复操作
Vim global命令和重复操作 Vim global命令允许我们在某个指定模式的所有匹配行上运行可执行的 Ex 命令,缩写形式为 :g,其处理重复工作的效率极高. 一.Vim global命令介绍 ...
- Android消息通知-Notification
Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...
- Search a 2D Matrix leetcode java
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- [Backbone] Working with forms
Our first step is to add a template to the AppointmentForm below. Have the template produce a form w ...
- Java 程序内存分析
转自:http://www.iteye.com/topic/528230 java程序内存主要分为了2个部分,包括stack segment(栈内存区).heap segment(堆内存区). 在分析 ...
- 提高ASP.NET首页性能的十大方法
本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分: http://www.cnblogs.com/xiachufeng/archive/2011/11/09/2242130.h ...
- linux查询文件中某几行
查询文件中某几行: sudo cat /etc/tinyproxy.conf | head -n | tail -n + [一]从第3000行开始,显示1000行.即显示3000~3999行 cat ...
- php之快速入门学习-5(常量)
PHP 5 常量 常量值被定义后,在脚本的其他任何地方都不能被改变. PHP 常量 常量是一个简单值的标识符.该值在脚本中不能改变. 一个常量由英文字母.下划线.和数字组成,但数字不能作为首字母出现. ...
- 在MyEclipse中设置jsp页面为默认utf-8编码(转)
http://www.cnblogs.com/xdp-gacl/p/3496161.html 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是“ISO-8859-1”,如下图所示: 在这种 ...
- Oracle function实现根据输入的日期以及天数,获取此日期之后的天数的工作日
前提:在法定节日表(t_fdjr )中维护法定节日包括周六周天 /** * 功能描述:根据输入的日期以及天数,获取此日期之后的天数的工作日 * 输入参数: * i_date YYYY-MM-DD * ...