Constructing Roads
Time Limit: 2000MS   Memory Limit: 65536K
             

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

找的最小生成树专题里的一个题,看到这题却发现和河南省第八届省赛的“引水工程”惊人的相似,反正那道题也没做出来,本打算先A了那道题再做这道题,可是做了那题之后又发现这道题还更简单,于是调调代码直接A了,其实我宁愿相信是后台测试数据水的;

来说说思路吧:我们发现输入是以矩阵的模式,如果全部存在结构体中无疑是浪费内存,我们发现这个矩阵中很多相同的数,即坐标相对应的点(如a[i][j]=a[j][i])的值是一样的,所以只需要把矩阵中的一半的数存在结构体中就可以了,然后就是最简单的最小生成树了,至于有Q条已经联通好的路我们只需在输入得时候利用并查集将输入的数据的根节点合并即可;可能这里表达有点不清楚,来看代码就马上明白了;

AC代码:说白了,这题就是并查集与最小生成树的灵活运用;

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=5100;//数据范围是100,平方是10000,但Q<=10000/2,开5050就可以过了;
int n,m,k,f[100];
struct node
{
int u,v,w;
} a[N];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
return f[x]==-1?x:x=find(f[x]);
}
int ks(int n)
{
int ans=0,cot=0;
sort(a+1,a+1+k,cmp);
for(int i=1; i<=k; i++)
{
int u=find(a[i].u);
int v=find(a[i].v);
if(u!=v)
{
ans+=a[i].w;
f[u]=v;
cot++;
}
if(cot==n-1)
break;
}
return ans;
}
int main()
{
int x;
scanf("%d",&n);
k=0;
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
scanf("%d",&x);
if(j>i)//把一半的数据存在结构体中;
a[++k].u=i,a[k].v=j,a[k].w=x;
}
memset(f,-1,sizeof(f));
scanf("%d",&m);
int x1,y1;
while(m--)
{
scanf("%d%d",&x1,&y1);
int xx=find(x1);
int yy=find(y1);
if(xx!=yy)//根节点合并->并查集;
{
f[xx]=yy;
n--;
}
}
printf("%d\n",ks(n));
return 0;
}

POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~的更多相关文章

  1. 第八届河南省赛D.引水工程(kruthcra+prime)

    D.引水工程 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 118  Solved: 41 [Submit][Status][Web Board] D ...

  2. 第八届河南省赛F.Distribution(水题)

    10411: F.Distribution Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 11  Solved: 8 [Submit][Status] ...

  3. 第八届河南省赛G.Interference Signal(dp)

    G.Interference Signal Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 17 [Submit][Status ...

  4. 第八届河南省赛C.最少换乘(最短路建图)

    C.最少换乘 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 94  Solved: 25 [Submit][Status][Web Board] De ...

  5. 第八届河南省赛B.最大岛屿(dfs)

    B.最大岛屿 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 30  Solved: 18 [Submit][Status][Web Board] De ...

  6. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  7. poj1861 network(并查集+kruskal最小生成树

    题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...

  8. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. hdu 1233 还是畅通工程 并查集or最小生成树

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...

随机推荐

  1. Linux下文件权限的设置

    文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 ) 其语法格式为:chmod ...

  2. jmeter(十八)属性和变量

    一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...

  3. [未读]backbonejs应用程序开发

    买来就没有动过,那阵子刚好离职找工作,之后学backbone的劲头就过去了= =

  4. Suricata产生的数据存储目录

    不多说,直接上干货! 我这里呢,分两种常用的Suricata. 一.源码编译安装的Suricata 这里不多说,大家可以去看我下面写的博客 使用 Suricata 进行入侵监控(一个简单小例子访问百度 ...

  5. qconbeijing2015

    http://2015.qconbeijing.com/schedule 大会日程 2015年4月23日,星期四 地点 2号厅 203AB 201AB 9:15 开场致辞 专题 主题演讲 互联网金融背 ...

  6. 职业生涯手记——记人生中第一次经历的产品上线——内测篇Day11

    2017/08/21 产品内测期Day11 说出来可能你不信,原定于9月15号结束的内测活动,今天居然被甲方投诉导致强制停止,原因是这个内测活动没有经过批准,并且有用户打了甲方所在公司的客服部门,增加 ...

  7. QT 学习笔记概述

    以下笔记为在看书和实践的过程中的部分记录总结: 0. 窗口布局 1) 支持绝对布局和布局管理器布局; 2) 绝对布局不够灵活.无法自动调整大小,需要手动编写代码调整: 3) 布局管理器管理布局比较灵活 ...

  8. react开启一个项目 webpack版本出错

    npx create-react-app my-app cd my-app npm start 在命令行里执行以上语句就可(前两天刚刚发现,最新版的react对webpack的版本要了新要求,大概是他 ...

  9. 区间DP || HDU 6249 Alice’s Stamps

    题意:标号为1-n的n种邮票,m个邮票集,每个集里有标号从Li到Ri的邮票,要从中选K个邮票集,使这K个邮票集能覆盖最多种的邮票,问最多能覆盖多少种邮票 思路:区间DP (我:??? f[i][j]表 ...

  10. Caused by: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.bj186.crm.pojo.User

    原因分析: myBatis在写delete语句的时候,多写了一个属性 错误的代码: <delete id="deleteUser" parameterType="I ...