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. uva1587 Box

    Ivan works at a factory that produces heavy machinery. He has a simple job -- he knocks up wooden bo ...

  2. Mysqldump记录

    MySql导出特定的一段记录(导出为SQL语句) mysqldump –u root -p 数据库名 表名 --where=" author like '%Joking%' " & ...

  3. 如何在异步请求时设置RequestHeader

    一.为何要用到setRequestHeader 通常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTTP协议的头文件,告诉服务器客户端要下载什么信息以及相关的参数.而 XMLHTT ...

  4. initEvent vs initMouseEvent

    var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, ...

  5. LeetCode_Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  6. Source Insight设置总结

    在网上搜索了一些关于Source Insight的设置技巧,把这些结果给总结下来: 1. 背景色选择 要改变背景色Options->preference->windows backgrou ...

  7. Windows 8.1 with update 官方最新镜像汇总(全)

    Windows 8.1 with update 官方最新镜像汇总,发布日期: 2014/12/16,Microsoft MSDN. 镜像更新日志: 12/29:32位大客户专业版中文版12/24:64 ...

  8. Keil 中关于C语言编译生成汇编代码函数名规则

    在keil 中 C语言的函数有带参数和不带参数之分. 一般的资料里说fun(void)类型的函数不带参数,所以,keil编译器生成的汇编的调用地址(函数名) 为fun.这没有错.事实上,不管C语言的函 ...

  9. 单线多拨,傻瓜式openwrt单线多拨叠加速率教程

    http://bbs.pceva.com.cn/thread-98362-1-1.html

  10. JS中简单的this学习

        我在学习JS初期,在使用this的时候经常出现问题,当然就是在现在,也有一些场景不能很好的明白this到底指代的是什么?看下面一个例子:   var x = 10; var foo = { x ...