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. 优化Webstorm

    Webstorm这个编辑器还是很强大的,而且版本更新得快,支持最新的typescript,就是性能越来越低. 本文总结了一些优化Webstorm的有效方法,希望对大家有所帮助! 测试环境 Mac OS ...

  2. [C/C++基础] C语言常用函数sprintf和snprintf的使用方法

    Sprintf 函数声明:int sprintf(char *buffer, const char *format [, argument1, argument2, …]) 用途:将一段数据写入以地址 ...

  3. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  4. Mac 下安装tomcat

    一. 下载tomcat 首先要到tomcat官网去下载安装包,官网下载地址如下:http://tomcat.apache.org/download-70.cgi , 注意请下载飞windows版本.和 ...

  5. [wikioi2069]油画(贪心)

    题目:http://www.wikioi.com/problem/2069/ 分析: 首先这个问题比较复杂,涉及到两个重要的考虑点,一个是当前拿来的颜色是否保留,一个是若保留后那么应该把当前盘子的哪个 ...

  6. 转:浅谈CSS在前端优化中一些值得注意的关键点

    前端优化工作中要考虑的元素多种多样,而合理地使用CSS脚本可以在很大程度上优化页面的加载性能,以下我们就来浅谈CSS在前端优化中一些值得注意的关键点: 当谈到Web的“高性能”时,很多人想到的是页面加 ...

  7. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

  8. mysql中的if判断

    问题是这样的,有一张表(tb_class)专门保存班级的ID和班级的名字 另一张表是学生信息表(tb_stu),表中有一个字段叫classID,没有外键关联,现在要把 这张表刷新到另一个表tb_par ...

  9. 洛谷P1202 [USACO1.1]黑色星期五Friday the Thirteenth

    题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...

  10. UVA 11527 Unique Snowflakes

    用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...