HDU1233——还是通常工程(最小生成树,并查集)

http://acm.hdu.edu.cn/showproblem.php?pid=1233

题意:就是裸的最小生成树。这里用的是kruskal

使用的是并查集,将按距离排序的边,分别把点加到集合里。节点存在在集合里说明是环路。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; 

int n,s,k;
int par[105];
struct point {
    int x,d,y;
};
point p[5005];

int cmp(point a,point b)
{
     return a.d<b.d;

}
int find(int x) {
    return par[x] == x ? x : par[x] = find(par[x]);
}

void unite(int x, int y,int i) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    s+=p[i].d;
    k++;
    par[x] = y;
}
int main(){
    while(~scanf("%d",&n)&&n!=0){
        int m=n*(n-1)/2;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].d);
        }
        s=0;k=0;
        for(int i=1;i<=n;i++){
            par[i]=i;
        }
        sort(p,p+m,cmp);
        for(int i=0;i<m;i++){
            unite(p[i].x,p[i].y,i);
            if(k==n-1)break;
        }
        printf("%d\n",s);
    }
    return 0;
} 
时间: 2025-01-09 22:51:59

HDU1233——还是通常工程(最小生成树,并查集)的相关文章

畅通工程-最小生成树+并查集

原题链接:https://vjudge.net/problem/23261/origin 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现请你编写程序,计算出全省畅通需要的最低成本. Input 测试输入包含若干测试用例.每个测试用例的第1行给出评估的道路条数 N.村庄数目M ( < 100 ):随后的 N 行对应村庄间道路的成本,每行给

继续畅通工程-最小生成树+并查集

原题链接:https://vjudge.net/problem/23262/origin 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全省畅通需要的最低成本. Input 测试输入包含若干测试用例.每个测试用例的第1行给出村庄数目N ( 1< N < 100 ):随后的 N(N-1)/2

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1

最小生成树-并查集-Kruskal-zoj-2048-special judge

Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting som

洛谷P1547 Out of Hay 最小生成树 并查集

洛谷P1547 Out of Hay 最小生成树 并查集 路径压缩 #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <iomanip> using namespace std ;

CSP 201703-4 地铁修建【最小生成树+并查集】

问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁. 地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之间最多只有一条候选的隧道,没有隧道两端连接着同一个交通枢纽. 现在有n家隧道施工的公司,每段候选的隧道只能由一个公司施工,每家公司施工需要的天数一致.而每家公司最多只能修

bzoj 1050: [HAOI2006]旅行comf(最小生成树+并查集)

1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2405  Solved: 1282 [Submit][Status][Discuss] Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T ,求一条路径,使得路径上最大边和最小边的比值最小.如果S和T之间没有路径,输出"IMPOSS

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

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

hdu 1233 还是畅通工程 kruskal最小生成树并查集实现

http://acm.hdu.edu.cn/showproblem.php?pid=1233 杭电ACM暑期集训队的选拔 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30319    Accepted Submission(s): 13542 Problem Description 某省调查乡村交通状况,得到的统计表中列

HDU ACM : 1875 畅通工程再续-&gt;最小生成树(并查集)

解析:最小生成树:Kruskal 算法:并查集实现. 1.首先找出符合要求的边: 2.对找出的边排序: 3.并查集找出n-1条边,无法修通n-1条路则无法实现要求. #include<iostream> #include<cmath> #include<algorithm> using namespace std; struct Point { int x,y; } point[102]; struct Edge { int a,b; double v; bool op