Constructing Roads(1102 最小生成树 prim)

时间:2022-09-17 13:09:29

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 &lpar;简单最小生成树Prim算法&rpar;

    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 &amp&semi; 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(最小生成树&lbrack;Prim&rsqb;)

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

随机推荐

  1. 【面经】【转】C程序的内存布局

    一个C语言程序一直以来都是由以下5个段组成: 1.代码段(text segmrnt):存放CPU执行的机器指令,通常情况下,代码段是可共享的,使其可共享的目的是对于频繁被执行的程序,只需要在没存中有有 ...

  2. 点击UITableviewCell展开收缩

    #import "ViewController.h" #import "ZSDTestCell.h" @interface ViewController ()& ...

  3. nopCommerce架构分析系列(二)数据Cache

    原文(http://www.cnblogs.com/gusixing/archive/2012/04/12/2443799.html)非常感谢作者顾思行的分享! 序言 在很多访问量较大的系统中,尤其在 ...

  4. structs2标签

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...

  5. 【转】CentOS图形界面的开启与关闭

    源自:http://blog.sina.com.cn/s/blog_4a1f76860100zpus.html 安装CentOS 5.6系统的时候我没有先装任何组件,现在用X Window,需要再安装 ...

  6. redis CONFIG REWRITE介绍

    可用版本为>= 2.8.0 CONFIG REWRITE 命令对启动 Redis 服务器时所指定的 redis.conf 文件进行改写: 因为 CONFIG SET 命令可以对服务器的当前配置进 ...

  7. linux&lpar;5&rpar;--补充(管道&vert; &sol; 重定向&gt&semi; &sol; xargs)&sol;find 与xargs结合使用&sol;vi&comma;grep&comma;sed,awk&lpar;支持正则表达式的工具程序&rpar;

    本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...

  8. 机器学习中的范数规则化-L0&comma;L1和L2范式(转载)

    机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http://blog.csdn.net/zouxy09 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...

  9. 关于 this 关键字的使用

    package com.jsti.guiyang_01; /* 自定义Phone类 this关键字 代表当前正在调用这个方法(访问成员变量)的对象(实例) 1.在setxxx方法中用来区分成员变量和局 ...

  10. Docker 系列四(自定义仓库)&period;

    一.Docker hub 交互 Docker hub 是 Docker 官方维护的一个公共仓库,大部分需求都可以通过在 Docker hub 中直接下载镜像来完成.接下来,来看一下怎么与 Docker ...