Constructing Roads

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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<stdio.h>
#include<algorithm>
using namespace std;
#define S 110
struct edge
{
int u , v , w ;
}e[S * S];
int n , m ,p[S];//这一这三个变量的位置要小心,不然,嘿嘿 void init ()
{
for(int i = ; i <= S ; i++)
p[i] = i ;
} bool cmp (edge a , edge b)
{
return a.w < b.w ;
} int find (int x)
{
return x == p[x] ? x : p[x] = find(p[x]) ;
} void kruskal ()
{
sort (e + , e + m , cmp) ;
int ans = , x , y ;
for (int i = ; i < m ; i++) {
x = find (e[i].u) ;
y = find (e[i].v) ;
if (x != y) {
ans += e[i].w ;
p[x] = y ;
}
}
printf ("%d\n" , ans) ;
}
int main ()
{
// freopen ("a.txt" , "r" ,stdin) ;
int k , built ;
int x , y ;
while (~ scanf ("%d" , &n ) ) {
init () ;
m = ;
for (int i = ; i <= n ; i++) {
for (int j = ; j <= n ; j++) {
if (j <= i) {
scanf ("%d" , &k ) ;
continue ;
}
scanf ("%d" , &e[m].w) ;
e[m].u = i ;
e[m].v = j ;
m++ ;
}
}
scanf ("%d" , &built );
for (int i = ; i < built ; i++) {
scanf ("%d%d" , &x , &y ) ;
x = find (x) ;
y = find (y) ;
p[x] = y ;
}
kruskal () ;
}
return ;
}

以前碰到“以建”这种问题,我就只会想到把w = 0 ;

但没想到还能把“以建”的两顶点,先串起来的方法,充分利用了kruskal的性质,不赞不行 <(= I =)>

自己火候不够

 

Constructing Roads (MST)的更多相关文章

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

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

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

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

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

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

  4. hdu 1102 Constructing Roads (Prim算法)

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

  5. Constructing Roads——F

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

  6. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  7. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. HDU 1102 Constructing Roads

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

  9. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

随机推荐

  1. RxJava简介

    RxJava简介 本文为前段时间学习RxJava时留下的历史遗留笔记,仅作纪念,科学的大神教学帖子在这里-> 给 Android 开发者的 RxJava 详解 通过链式调用序列实现基于事件流的异 ...

  2. Chrome扩展开发之四——核心功能的实现思路

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  3. iOS适配HTTPS,创建一个自签名的SSL证书(x509)具体步骤

    引言(创建生成的证书只能用于测试使用.如果想使用自签名证书就只能以自己为 CA机构颁发证书,进行双向认证才能使用) 使用HTTP(超文本传输)协议访问互联网上的数据是没有经过加密的.也就是说,任何人都 ...

  4. 【转载】cocs2dx中c++与c#互调

    文章有参考http://www.cnblogs.com/zhxilin/archive/2013/03/20/2971331.html 下面以接入九幽数据统计插件为例 Step 1:如果是cocos2 ...

  5. Android Studio修改项目的包名

    android studio的修改包名,没有有Eclipse环境中那么好操作.也可能你对Eclipse的操作比较熟悉,对Andoid Studio的操作还不太熟悉.在项目的开发中,你可能遇到需要更改模 ...

  6. 开源搜索引擎Iveely 0.7.0发布,不一样,那就让他不一样!

    2012年08月05日,Iveely Search Engine 0.1.0发布,今天,怀着对于未来的追求,终于,0.7.0如期和大家见面了,7个版本,历时2年4个月,感谢大家的支持,感谢我不离不弃的 ...

  7. Javascript基础系列之(六)循环语句(while语句)

    循环语句的作用是反复的执行同一段代码,尽管分几种不同的类型,但其原理几乎相同:只要给定的条件满足,包含在循环体内的语句会不断执行,一旦条件不再满足则终止. while循环是前测试循环,这意味着是否终止 ...

  8. angular的编辑器tinymce

    angular的插件的确挺少的, 编辑器更是少, ui-tinymce是angular-ui推荐的一款编辑器(GIT: https://github.com/angular-ui/ui-tinymce ...

  9. jQuery Mobile学习日记(二)

    首先依HTML5方式加载,DOCTYPE表示格式为HTML5:主要适用于iPhone.Android等,viewport表示适用于移动终端的适中显示,initial-scale缩放比例在1.0~1.3 ...

  10. Android开发环境的调研

    在大二下,由于课程需要,就去自学Java,当时就借了一本java面向对象的书来看,根据书上的说明搭建环境,下载了JDK1.6和eclipse.但从来没有用过eclipse,java也就只是刚入门而已. ...