并查集模版&最小生成树

A - 畅通工程
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。 
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。 
 
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。 
 
Sample Input
 3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100 
 
Sample Output
 3

?

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[1010];
int c;
struct e
{
    int x,y,z;
}s[1010];
int cmp(e a,e b)
{
    return a.z<b.z;
}
int sum,i,j;
int find(int x)
{
    if (f[x] == x)
        return x;
    else
        return (f[x] = find(f[x]));
}
void merge(int a, int b)
{
    int x, y;
    x = find(a);
    y = find(b);
    if (x != y)
        {f[x] = y;
        sum+=s[i].z;
        c++;
        }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
         sum=0;
         c=0;
        if(n==0) break;
        for(i=1;i<=m;i++)
        f[i]=i;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);
        }
        sort(s+1,s+n+1,cmp);
        for(i=1;i<=n;i++)
        {
            merge(s[i].x,s[i].y);
        }
       if(m-c-1==0)

        printf("%d\n",sum);
        else printf("?\n");
    }
}
B - 还是畅通工程
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。 
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。 
当N为0时,输入结束,该用例不被处理。 
 
Output
对每个测试用例,在1行里输出最小的公路总长度。 
 
Sample Input
 3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5

 
Sample Output
 3

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[5110];
int c;
struct e
{
    int x,y,z;
}s[5110];
int cmp(e a,e b)
{
    return a.z<b.z;
}
int sum,i,j;
int find(int x)
{
    if (f[x] == x)
        return x;
    else
        return (f[x] = find(f[x]));
}
void merge(int a, int b)
{
    int x, y;
    x = find(a);
    y = find(b);
    if (x != y)
        {f[x] = y;
        sum+=s[i].z;
        c++;
        }
}
int main()
{
    int n,m;
    while(scanf("%d",&m)!=EOF)
    {
         sum=0;
         c=0;
        if(m==0) break;
        n=m*(m-1)/2;
        for(i=1;i<=m;i++)
        f[i]=i;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);
        }
        sort(s+1,s+n+1,cmp);
        for(i=1;i<=n;i++)
        {
            merge(s[i].x,s[i].y);
        }
       if(m-c-1==0)

        printf("%d\n",sum);
        else printf("?\n");
    }
}

C - 畅通工程再续
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
 
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。 
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。 
 
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
 
Sample Input
 2
2
10 10
20 20
3
1 1
2 2
1000 1000 
 
Sample Output
 1414.2
oh!

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int bin[222];
int n;
int counta;
double result;
struct point
{
	int x;
	int y;
}p[222];
struct dis
{
	int a;
	int b;
	double distance;
}d[11111];

double liangdianjuli(point a,point b)
{
	double jl=sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
	return jl;
}

int cmp(dis a,dis b)
{
	if(a.distance<b.distance)
	{
		return 1;
	}
	return 0;
}

int find(int x)
{
    if(bin[x]!=x) bin[x]=find(bin[x]);
    return bin[x];
}
void merge(dis d,double &p)
{
    int a,b;
    a=find(d.a);
    b=find(d.b);
    if(a!=b)
    {
    	counta+=1;
        bin[a]=b;
        p+=d.distance;
    }

}
int main(int argc, char *argv[])
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i;

		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			bin[i]=i;
		}


		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
		}

		int j;

		int dw=0;

		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				double juli;
				juli=liangdianjuli(p[i],p[j]);
				if(juli>=10&&juli<=1000)
				{
					d[dw].a=i;
					d[dw].b=j;
					d[dw].distance=juli;
					dw+=1;
				}
			}
		}

	sort(d,d+dw,cmp);

	result=0;
	counta=1;
		for(i=0;i<dw;i++)
		{
			merge(d[i],result);
		}
	if(counta!=n)
	{
		printf("oh!\n");
	}
	else{
		printf("%.1lf\n",result*100);
	}


	}
	return 0;
}

D - 继续畅通工程
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。 
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。 


当N为0时输入结束。
 
Output
每个测试用例的输出占一行,输出全省畅通需要的最低成本。
 
Sample Input
 3
1 2 1 0
1 3 2 0
2 3 4 0
3
1 2 1 0
1 3 2 0
2 3 4 1
3
1 2 1 0
1 3 2 1
2 3 4 1

 
Sample Output
 3
1

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
int f[5510];
using namespace std;
struct e
{
    int a,b,c,d;
}s[5111];
int cmp(e a,e b){return a.c<b.c;}
int i,j,n,m;
int sum;
int find(int x)
{
    if (f[x] == x)
        return x;
    else
        return (f[x] = find(f[x]));
}
void merge(int a, int b)
{
    int x, y;
    x = find(a);
    y = find(b);
    if (x != y)
        {f[x] = y;

        sum+=s[i].c;
        }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        for(i=1;i<=n;i++)
        f[i]=i;
        sum=0;

        m=n*(n-1)/2;
       for(i=1;i<=m;i++)
       {
           scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].d);
           if(s[i].d==1) s[i].c=0;
       }
       sort(s+1,s+m+1,cmp);
       for(i=1;i<=m;i++){


            merge(s[i].a,s[i].b);
       }
       printf("%d\n",sum);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值