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. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. LibSvm介绍---调用方法及参数介绍

        libsvm是著名的SVM开源组件,目前有JAVA.C/C++,.NET 等多个版本,本人使用的是2.82 libsvm命名空间下主要使用类: svm_model 为模型类,通过训练或加载训练 ...

  3. shell脚本中局部变量local

    shell脚本中局部变量 在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变量和函数的变量的作用域问题可能令你费解,在这里梳理一下这个问题. (1)Shell脚本中定义的变量是gl ...

  4. 图片延迟加载scrollLoading.js应用

     <ul>                     <li><a href="http://news.qq.com/" target="_b ...

  5. UESTC_Dividing Numbers CDOJ 1156

    Dividing Numbers Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Other ...

  6. softlayer virtual machine vhd磁盘镜像导入shell脚本

    脚本

  7. c++中经常需要访问对象中的成员的三种方式

    可以有3种方法: 通过对象名和成员运算符访问对象中的成员; 通过指向对象的指针访问对象中的成员; 通过对象的引用变量访问对象中的成员. 一.通过对象名和成员运算符访问对象中的成员 例如在程序中可以写出 ...

  8. 【转】TI-Davinci开发系列之六CCS5.2调试Linux内核

    上转博文<TI-Davinci开发系列之五CCS5.2使用gdbserver远程调试应用程序> 使用CCS5.2远程调试内核时,只需导入Linux内核源码,而不需要编译内核,也就不会用到交 ...

  9. 【转】Android NDK开发入门实例

    写这个,目的就是记录一下我自己的NDK是怎么入门的.便于以后查看,而不会忘了又用搜索引擎一顿乱搜.然后希望能够帮助刚学的人入门. 先转一段别人说的话:“NDK全称:Native Development ...

  10. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...