zoukankan      html  css  js  c++  java
  • HDU 4424 Conquer a New Region【并查集】【思维题】

    Conquer a New Region

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2089    Accepted Submission(s): 737


    Problem Description
    The wheel of the history rolling forward, our king conquered a new region in a distant continent.
    There are N towns (numbered from 1 to N) in this region connected by several roads. It's confirmed that there is exact one route between any two towns. Traffic is important while controlled colonies are far away from the local country. We define the capacity C(i, j) of a road indicating it is allowed to transport at most C(i, j) goods between town i and town j if there is a road between them. And for a route between i and j, we define a value S(i, j) indicating the maximum traffic capacity between i and j which is equal to the minimum capacity of the roads on the route. 
    Our king wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other N - 1 towns is maximized. Now, you, the best programmer in the kingdom, should help our king to select this center.
     

    Input
    There are multiple test cases.
    The first line of each case contains an integer N. (1 <= N <= 200,000)
    The next N - 1 lines each contains three integers a, b, c indicating there is a road between town a and town b whose capacity is c. (1 <= a, b <= N, 1 <= c <= 100,000)
     

    Output
    For each test case, output an integer indicating the total traffic capacity of the chosen center town.
     

    Sample Input
    4 1 2 2 2 4 1 2 3 1 4 1 2 1 2 4 1 2 3 1
     

    Sample Output
    4 3
     

    Source


    按照边权的大小从大到小排序,然后每次选最大的边,判断两个点,把他加到并查集,并每次优化两个点到另一个的值是多少,将集合更新为大的(相当于让尽可能多的点通过这个大的边),当时写的时候想到树形dp上去了,讨论了好久没有写出来,这个题思路还是比较重要的。


    #include<iostream>	
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    const int maxn = 200010;
    const int mod = 1e9 + 7;
    const double pi = acos(-1.0);
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    
    int pre[maxn], num[maxn], n;
    ll sum[maxn], ans;
    
    struct Node {
    	int u, v;
    	ll w;
    }a[maxn];
    
    int find(int x)
    {
    	if (x == pre[x]) return x;
    	return pre[x] = find(pre[x]);
    }
    
    void init()
    {
    	for (int i = 1; i <= n; i++)
    	{
    		pre[i] = i;
    		num[i] = 1;
    		sum[i] = 0;
    	}
    }
    
    bool cmp(Node a, Node b)
    {
    	return a.w > b.w;
    }
    
    void unite(int a, int b, ll w)
    {
    	a = find(a);
    	b = find(b);
    	ll f1 = sum[a] + num[b] * w;
    	ll f2 = sum[b] + num[a] * w;
    	if (f1 > f2)
    	{
    		pre[b] = a;
    		num[a] += num[b];
    		sum[a] = f1;
    	}
    	else
    	{
    		pre[a] = b;
    		num[b] += num[a];
    		sum[b] = f2;
    	}
    }
    
    
    int main()
    {
    	while (~scanf("%d", &n))
    	{
    		for (int i = 0; i < n-1; i++)
    		{
    			scanf("%d%d%lld", &a[i].u, &a[i].v, &a[i].w);
    		}
    		init();
    		sort(a, a + n - 1, cmp);
    		for (int i = 0; i < n - 1; i++)
    		{
    			unite(a[i].u, a[i].v, a[i].w);
    		}
    		printf("%lld
    ", sum[find(1)]);
    	}
    }



    Fighting~
  • 相关阅读:
    MS17-010(永恒之蓝)远程溢出漏洞
    SQL注入实战之盲注篇(布尔、时间盲注)
    kali下vulhub的使用
    SQL注入实战之报错注入篇(updatexml extractvalue floor)
    SQL注入实战之联合查询篇(含漏洞网站搭建)
    apache/nginx/IIS日记记录分析
    nvcatmysql的安装
    乌云(wooyun)虚拟机的搭建复活
    Web安全基础笔记
    公告、留言
  • 原文地址:https://www.cnblogs.com/Archger/p/8451634.html
Copyright © 2011-2022 走看看