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

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

PKU Monthly,kicc

找的最小生成树专题里的一个题,看到这题却发现和河南省第八届省赛的“引水工程”惊人的相似,反正那道题也没做出来,本打算先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;
}
时间: 2024-10-08 14:29:41

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

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

D.引水工程 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 118  Solved: 41 [Submit][Status][Web Board] Description 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工程,事关中华民族长远发展. “南水北调工程”,旨在缓解中国华北和西北地区水资源短缺的国家战略性工程.就是把中国长江流域丰盈的水资源抽调一部分送到华北和西北地区.我国南涝北

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

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to b

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

还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26860    Accepted Submission(s): 11985 Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路

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

还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26860    Accepted Submission(s): 11985 Problem Description 某省调查乡村交通状况,得到的统计表中列出了随意两村庄间的距离.省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交

HDU3938 Portal (并查集经典+最小生成树)

本题从题目给出的条件我们发现了最小生成树的影子,也就是kruscal的影子 其实我们在写kruscal的时候就是利用并查集的思想来写的 这题需要注意的是,我们在求取的过程中l并不需要减少,我们只要最小生成树中的最大边权小于等于l就行了 这就让我们想到了可以从小到大对边权排序,之后枚举维护树集合,如果两个点还不在同一颗树中那就合并 那么答案就是求取的路径的个数,我们知道每个边对路径的个数的贡献就是左边节点?右边节点,根据乘法原理可得. 本题还需要注意的是,询问的个数很多,如果在线查询,每次从头开始

POJ 2421--Constructing Roads【水题 &amp;&amp; 最小生成树 &amp;&amp; kruskal】

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20889   Accepted: 8817 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

hdu1875 畅通工程再续 并查集+kruskal最小生成树

Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米.当然,为了节省资金,只要求实现任意2个小岛之间有路通即可.其中桥的价格为 100元/米.

Ghostbusters(并查集,最小生成树)

Ghostbusters 时间限制: 1 Sec  内存限制: 128 MB提交: 33  解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 The Bureau of Approved Peripherals for Computers (BAPC) is designing a new standard for computer keyboards. With every new norm and regulation, hardware becomes obsol

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

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 8315 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