HDU 4424 并查集

点击打开链接

题意:给n个城镇,然后每两个城镇的价值给出,但是u->v的价值是u->v这条路径上的最小值,问从任意一个点出发到其他位置的和的最大值

思路:因为限制条件是路径上的最小值,那么我们按价值排序,从大到小排,就可以避免这个问题了,然后利用并查集来完成,对于两个集合,左集合的价值总和为A,右集合的价值总和为B,现在有一条路径连接这两个集合,那么我们考虑的是将A合并到B还是将B合并到A,我们可以这么比较,连接这两个集合的路径肯定是目前最小的价值,如果A集合元素个数为Ai,那么将A合并到B则B需要加A*Ai,反过来A则加B*Bi,比较哪个大就行了,因为它们一定是合并成一块了,价值大的留下

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=200010;
int f[maxn];
ll num[maxn],r[maxn];
struct edge{
    int u,v;
    ll cost;
}es[maxn];
bool cmp(const edge &a,const edge &b){
    return a.cost>b.cost;
}
int find1(int x){
    if(x!=f[x]) f[x]=find1(f[x]);
    return f[x];
}
ll slove(int n,int k){
    for(int i=0;i<=n;i++) num[i]=0;
    for(int i=0;i<k;i++){
        int u=find1(es[i].u);
        int v=find1(es[i].v);
        if(num[u]+r[v]*(es[i].cost)>=num[v]+r[u]*(es[i].cost)){
            f[v]=u;num[u]+=r[v]*es[i].cost;
            r[u]+=r[v];
        }else{
            f[u]=v;num[v]+=r[u]*es[i].cost;
            r[v]+=r[u];
        }
    }
    return num[find1(1)];
}
int main(){
    int n,u,v;
    ll cost;
    while(scanf("%d",&n)!=-1){
        for(int i=0;i<=n;i++){
            f[i]=i;
            r[i]=1;
        }
        int k=0;
        for(int i=0;i<n-1;i++){
            scanf("%d%d%I64d",&u,&v,&cost);
            es[k].u=u;es[k].v=v;es[k++].cost=cost;
        }
        sort(es,es+k,cmp);
        ll ans=slove(n,k);
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-11-17 15:27:53

HDU 4424 并查集的相关文章

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

HDU 3938 并查集

求小于L的路径点对数(路上的最大值),按权值排序,从小到大并查集建图,有点kruskal的意思. /** @Date : 2017-09-22 17:30:11 * @FileName: HDU 3938 并查集 离线.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h>

HDU 1829 并查集

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8224    Accepted Submission(s): 2631 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp