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
 

 
Source
kicc
 

Recommend
Eddy

最小生成树的模板题目

下面的代码使用了Prim算法

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#define INF 0x7ffffff
#define MAXN 200
using namespace std;
const double eps=1e-;
const double PI=acos(-);
int G[MAXN][MAXN];
int vnew[MAXN];
int lowval[MAXN];
int sum;
int n,q;
void Prim(int start)
{
int j,mi;
for(int i=;i<=n;i++){
if(i!=start){
lowval[i]=G[start][i];
vnew[i]=;
}
}
vnew[start]=;
for(int i=;i<=n-;i++){
j=-;
mi=INF;
for(int i=;i<=n;i++){
if(vnew[i]==&&lowval[i]<mi){
j=i;
mi=lowval[i];
}
}
vnew[j]=;
sum+=lowval[j];
for(int i=;i<=n;i++){
if(vnew[i]==){
lowval[i]=min(lowval[i],G[j][i]);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
//Prim算法
int a,b;
while(cin>>n){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>G[i][j];
}
}
cin>>q;
for(int i=;i<q;i++){
cin>>a>>b;
G[a][b]=G[b][a]=;
}
sum=;
Prim();
cout<<sum<<endl;
} }

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

  1. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

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

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  3. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

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

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

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

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

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

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

  7. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...

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

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  10. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

随机推荐

  1. c# new关键字的三种用法

    三种用法如下: 在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数. 2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成 ...

  2. BestCoder Round #85 A B C

    本来没有写博客的打算,可是看完了题解感觉这三道题这么水,我却只做出来一道,实在不应该,还是写点东西吧…… A.sum 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出N ...

  3. 关闭HTC手机充电时屏幕一直亮着绿色电池的办法

            首先必须取得ROOT权限,用RE文件管理器进system/bin/把最下面的zchgd文件改名字或者直接删掉重新启动手机充电时就不会亮屏了,电池图标也不会出来,再用数据线连电脑充电吧, ...

  4. mnesia

    1.模式创建 mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式 2.启动和停止 application:start(mnesia). applica ...

  5. 去掉matlab图片空白边缘

    在图形文件figure的菜单上点击file->export setup size选项中,对"expand axes to fill figure"选项打勾,如下图:

  6. php fsockopen例子

    <?php /* * 短信相关api */ class Webcall { protected $VCC_CODE; //api的账号 protected $password; //api的密码 ...

  7. C# WebService 概念,创建及引用调用

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  8. javaWEB总结(2): load-on-startup节点

    在javaWEB总结(1)里面,我们创建了一个servlet以及它的映射,当我们访问那个映射地址的时候,就可以访问servlet,并且servlet容器调用了生命周期方法,销毁前调用destroy方法 ...

  9. play框架概述

    今天是来百度实习的第四天,在项目过程中mentor说会用到play框架,当时就一个晕啊.Java还有一个叫play框架,作为一个从大三开始用java的重度javaer,居然不知道这个框架的存在,内心一 ...

  10. iOS不可变字符串的所有操作

    可以直接复制代码即可运行看看结果,方便理解 //NSString //OC字符串不能用printf输出 //但是遗憾的是OC没有一个从终端读取数据的方式,需要使用scanf读取C字符串然后转换成OC的 ...