zoukankan      html  css  js  c++  java
  • hdu 1233 还是畅通project (克鲁斯卡尔裸题)

    还是畅通project

                                                 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                         Total Submission(s): 32795    Accepted Submission(s): 14769



    Problem Description
    某省调查乡村交通状况。得到的统计表中列出了随意两村庄间的距离。省政府“畅通project”的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连。仅仅要能间接通过公路可达就可以),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
     

    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 0
     

    Sample Output
    3 5 Huge input, scanf is recommended.
     

    Source

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233

    解题思路:裸的最小生成树,克鲁斯卡尔,边权值排序。

    代码例如以下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define inf 1e9
    const int maxn=10005;
    int fa[maxn];
    struct EG
    {
    	int x,y,w;
    }eg[maxn];
    void get_fa()
    {
     	for(int i=0;i<=maxn;i++)
     		fa[i]=i;
    }
    int find(int x)
    {
    	return x==fa[x]?x:find(fa[x]);
    }
    void Union(int a,int b)
    {
    	int a1=find(a);
    	int b1=find(b);
    	if(a1!=b1)
    		fa[a1]=b1;
    }
    int cmp(EG a,EG b)
    {
    	return a.w<b.w;
    }
    int main(void)
    {
    	int a,b,w,n;
    	while(scanf("%d",&n)!=EOF&&n)
    	{
    		get_fa();
    		int m=(n*(n-1))/2,ans=0;
    		for(int i=0;i<m;i++)
    			scanf("%d%d%d",&eg[i].x,&eg[i].y,&eg[i].w);
    		sort(eg,eg+m,cmp);
    		for(int i=0;i<m;i++)
    		{
    			if(find(eg[i].x)!=find(eg[i].y))
    			{
    				Union(eg[i].x,eg[i].y);
    				ans+=eg[i].w;
    			}
    		}
    		printf("%d
    ",ans);
    	}
    }


  • 相关阅读:
    Linux2.6X内核中文件相关结构体总结
    Linux 内核文件系统与设备操作流程分析
    在linux下删除的共享文件怎么恢复
    fedora17的U盘安装和硬盘安装
    cakephp
    【25.00%】【vijos P1907】飞扬的小鸟
    【14.36%】【codeforces 614C】Peter and Snow Blower
    【14.67%】【codeforces 615D】Multipliers
    【records】10.24..10.30
    【非常高%】【codeforces 733A】Grasshopper And the String
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6977917.html
Copyright © 2011-2022 走看看