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. 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆

    原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...

  2. Enum扩展及MVC中DropDownListFor扩展方法的使用

    public enum SearchState { /// <summary> /// 全部 /// </summary> [Description("全部" ...

  3. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  4. 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  5. Java基础-String 存储机制管理

    JVM运行的时候,将内存分为两个部分,一部分是堆,一部分是栈.堆中存放的是创建对象,而栈中存放的则是方法调用过程中的局部变量或引用.在设计JAVA字符串对象内存实现的时候,在堆中又开辟了一块很小的内存 ...

  6. BZOJ-1015 StarWar星球大战 并查集+离线处理

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Submit: 4105 Solved: 1826 [Submit ...

  7. angularjs的页面拆分思想

    //app.js angular.module('MyModule', ['SubModule1', 'SubModule2']) .module('SubModule1', ['CommonModu ...

  8. 洛谷P2726 阶乘 Factorials

    题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...

  9. configure new linux

    vim   http://www.cnblogs.com/wswang/p/5088078.html zsh  sh -c "$(curl -fsSL https://raw.github. ...

  10. 更改动软代码生成器模板 验证Model数据合法性

    1.第一个模板 判断字段是否为空 类 IsNullableType.cmt static public partial class CommonType { public static bool Is ...