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. UITableView和UICollectionView的Cell高度的几种设置方式

    UITableViewCell 1.UITableView的Cell高度默认由rowHeight属性指定一个低优先级的隐式约束 2.XIB中可向UITableViewCell的contentView添 ...

  2. flatbuffers 使用问题记录

    1. 命名空间的问题 ----------------------------- namespace 1.0.3 版本包含文件类型前面不需要加命名空间,但是1.1.0 中包含需要在类型前加命名空间 i ...

  3. http请求过程简要

    一次http请求主要分为3个大步. 建立tcp连接. 这里就发生了经典的tcp三次握手.做个类比解释下,tcp好比http的秘书,和厂家(服务器端)做买卖.老板(http)叫秘书(tcp)去联系一下, ...

  4. [USACO]6.1.3 cow xor(二进制+Trie)

    题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...

  5. words in view Moqui resource code

    annotation:注释 注解 documentation:文件  证明文件 embed:嵌入 context:环境  上下文 explicity: 明确的 明白的 conversion: 转化

  6. 今天学习到的关于mysql数据库的linux命令

    1. 登录mysql数据库: mysql -uroot -p 2.安装会提示的mysql的数据库软件:mycli sudo apt-get install mycli 3.安装依赖包: sudo ap ...

  7. linux查看cpu、内存、版本信息

    1.  查看物理CPU的个数#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc –l 2.   查看逻辑CPU的个数#cat ...

  8. iOS开发小技巧--UIScrollView内部子控件添加约束的注意点

    注意:用UIScrollView时布局子控件的时候,不要相对于UIScrollView来添加约束,这样做不是设置子控件的位置,反而是设置了UIScrollView的contentSize 子控件的尺寸 ...

  9. Html-Css-a标签的使用

    a标签去掉下划线 a{ text-decoration:none; } 或者把这个属性分别加到a标签下, a:link{ text-decoration:none; } a:visited{ text ...

  10. web开发(二十一)之自定义拦截器的使用

    转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...