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

题解:把i,j值记录到结构体中的x,y 然后输入的值为修路的权值,然后进行最小生成树的裸题了

注意!注意!注意!

这个题多组输入,不然会WA

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
int x,y;
int val;
}road[10000005];
int pre[10000005];
int find(int x)
{
if(x==pre[x])
return x;
else
{
return pre[x]=find(pre[x]);
}
}
bool merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return true;
}
else
{
return false;
}
}
bool cmp(node x,node y)
{
return x.val<y.val;
}
int main()
{
int n; while(scanf("%d",&n)!=EOF)
{ for(int t=1;t<=n;t++)
{
pre[t]=t;
}
int dis;
int s=1;
for(int t=1;t<=n;t++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&dis);
if(j>t)
{
road[s].x=t;
road[s].y=j;
road[s].val=dis;
s++;
} }
}
sort(road+1,road+s+1,cmp);
int q;
scanf("%d",&q);
int fx,fy;
int cnt=0;
for(int t=1;t<=q;t++)
{
scanf("%d%d",&fx,&fy);
if(merge(fx,fy))
{
cnt++;
}
}
long long int sum=0;
for(int t=1;t<=s;t++)
{
if(cnt==n-1)
break;
if(merge(road[t].x,road[t].y))
{
cnt++;
sum+=road[t].val;
}
}
printf("%lld\n",sum);
}
return 0;
}

POJ 2421 Constructing Roads(最小生成树)的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  3. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

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

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

  5. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  6. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

  7. Poj 2421 Constructing Roads(Prim 最小生成树)

    题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...

  8. POJ - 2421 Constructing Roads(最小生成树&并查集

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  9. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

随机推荐

  1. mysql的安装以及简单的命令符

    在百度当中输入mySQL就可以下载了. 我们只需要一路的点击next就好了,注意,我们在安装的过程当中它会问我们是否要安装路径,我么要选择是. 在显示安装完成之后呢,我们会看到一个复选框,上面写着是否 ...

  2. [hadoop入门]mapper与reducer(word_count计数demo)

    1.mapper #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words = line.sp ...

  3. Codeforces 719E (线段树教做人系列) 线段树维护矩阵

    题面简洁明了,一看就懂 做了这个题之后,才知道怎么用线段树维护递推式.递推式的递推过程可以看作两个矩阵相乘,假设矩阵A是初始值矩阵,矩阵B是变换矩阵,求第n项相当于把矩阵B乘了n - 1次. 那么我们 ...

  4. Codeforces 1097F Alex and a TV Show (莫比乌斯反演)

    题意:有n个可重集合,有四种操作: 1:把一个集合设置为单个元素v. 2:两个集合求并集. 3:两个集合中的元素两两求gcd,然后这些gcd形成一个集合. 4:问某个可重复集合的元素v的个数取模2之后 ...

  5. C++面向对象类的实例题目一

    在一个程序中,实现如下要求: (1)构造函数重载 (2)成员函数设置默认参数 (3)有一个友元函数 (4)有一个静态函数 (5)使用不同的构造函数创建不同对象 code: #include<io ...

  6. 在Oracle 12C中使用scott账号

    在Oracle11g中默认是有scott账号的,但在Oracle 12C中则不能直接使用. 我的机器环境: 操作系统:Windows Server 2008 R2 64位 Oracle版本:Oracl ...

  7. Windows系统 安装 CMake

    Windows系统 安装 CMake 我们的电脑系统:Windows 10 64位 安装的CMake 版本:cmake-3.6.1-win64-x64(目前最新) 下载 在CMake官网下载:cmak ...

  8. PCL—点云分割(邻近信息) 低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5000147.html 分割给人最直观的影响大概就是邻居和我不一样.比如某条界线这边是中华文明,界线那边是西方文,最 ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)

    一. 1. 2.

  10. Java Calendar 类的时间操作.RP

    JavaCalendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单. 演示了获取时间,日期时间的累加和累减,以及比较. 原文地址:blog.csdn.NET/joyous/a ...