Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18256    Accepted Submission(s): 6970

Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village 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.

 
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village 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.

 
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
int map[][];
int n,q,a,b;
int res;
int mincost[];
int vis[];
void prim()
{
mincost[]=;
while(true)
{
int v=-;
int u;
for(u=;u<=n;u++)
{
if(!vis[u]&&(v==-||mincost[u]<mincost[v]))
v=u;
}
if(v==-) break;
vis[v]=;
res+=mincost[v];
for(u=;u<=n;u++)
mincost[u]=min(map[v][u],mincost[u]);
}
return;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
res=;
fill(mincost,mincost+,INF);
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]);
scanf("%d",&q);
for(i=;i<q;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=;
}
prim();
printf("%d\n",res);
}
}
 

Constructing Roads(1102 最小生成树 prim)的更多相关文章

  1. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  2. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  3. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  4. hdu oj1102 Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  6. Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  7. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  8. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

  9. HDU-1301 Jungle Roads(最小生成树[Prim])

    Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. keepalived+httpd 做web服务的高可用

    场景: 环境中有两台httpd服务器,一台做主,一台做备用:平时只用主向外提供http服务:当主宕机后,keepalived把vip绑定到备机上去,这样就由备机提供http服务了. **keepalv ...

  2. 用户子查询,用case

    select  satisfy.STARTTIME,cc.C_CrmID,cc.C_Name ClientName,be.C_NAME,count(yskj.c_id) PhoneSum,sum(ca ...

  3. Linux下SVN(Subversion)自动启动脚本

    在Red Hat  Linux中自动运行程序    1.开机启动时自动运行程序    Linux加载后, 它将初始化硬件和设备驱动,然后运行第一个进程init.init根据配置文件继续引导过程,启动其 ...

  4. Powershell --在线学习

    介绍和安装 自定义控制台 快速编辑模式和标准模式 快捷键 管道和重定向 Powershell交互式 数学运算 执行外部命令 命令集 别名 通过函数扩展别名 执行文件和脚本 Powershell变量 定 ...

  5. c++ 09

    一.数据结构 程序设计=数据结构+算法 1.逻辑结构 1)集合:元素之间没有联系. 2)线性结构:元素之间存在前后顺序. 3)树形结构:元素之间存在一对多的父子关系. 4)图状结构:元素之间存在多对多 ...

  6. 商派shopex

    http://www.shopex.cn/48release/shopexsingle_exper.php 在线体验 前台体验:http://demo.shopex.com.cn/485 后台体验:h ...

  7. Openstack no valid hot

    错误: 创建实例 "ce" 失败: 请稍后再试 [错误: No valid host was found. ].

  8. linux之getcwd函数解析

    [lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...

  9. Linux硬盘分区和格式化

    分区与格式化   先用fdisk分区,分区完成后再用mkfs格式化并创建文件系统,挂载,磁盘就能使用啦.   分区的原理:        MBR:主引导扇区 主分区表:64bytes,最多只能分四个主 ...

  10. ios想要取消执行延时调用的方法

    想要取消执行延时调用的方法: [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideDia ...