zoukankan      html  css  js  c++  java
  • HDU 4424 并查集+贪心思想

    Conquer a New Region

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



    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
    2012 Asia ChangChun Regional Contest

    题意:n个点,n-1条边,肯定就是一个树了,用并查集的话,一个树就是一个集合,这个题加上贪心思想觉得还是比较难的。

    思路:
    由于要所有点到这个点的权值和最大,把边按从大到小排序并插入。每条边连接两个集合,且每次并入的边权值都是当前已并入边中最小的。那么,只要每次并入时判断是把a并入b得到的权值和大,还是b并入a得到的权值和大就可以了。并查集维护集合的元素个数和总的权值。看了网上的觉得没有一个讲的详细的,决定在这里用数据模拟一下。

    数据:

    5
    3 2 1
    2 1 2
    4 2 3
    1 5 4
    经过排序后:
    5
    1-5 4
    4-2 3
    2-1 2
    3-2 1
     

    模拟一边之后就知道该算法的基本思想,详情看代码注释:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn =200000+10;
    typedef long long LL; //自定义类型
    int f[MAXN];
    LL sum[MAXN];
    int cnt[MAXN];
    struct Edge{
       int u,v,w;
    }ee[MAXN];
    int n;
    
    void Initiate()
    {
       memset(sum,0,(n+2)*sizeof(sum[0]));
       for(int i=0;i<=n;i++){
          f[i]=i;
          cnt[i]=1;//开始的每个点相当于一个树,每棵树只有一个节点
       }
    }
    
    int Find(int x)
    {
       if(x==f[x])
          return x;
       f[x]=Find(f[x]);
       return f[x];
    }
    
    void Union(int r1,int r2,LL w)
    {
       f[r1]=r2;
       cnt[r2]+=cnt[r1];//新连的边到各个点的条数,因为新加边肯定最小,所以在tmp中新加的w直接乘与路程数目,再加上自己的sum,可以得到新的总权值
       sum[r2]+=w; //tmp中r2和r1相反,如果是以新加点为中心,则加的是中心点以前的sum和,就比如还是以老根为中心,要加的上个sum就是这个,而cnt[新点]*w则是新加的
    }
    
    int cmp( Edge p, Edge q)
    {
       return p.w>q.w;
    }
    
    int main()
    {
       //freopen("in.txt","r",stdin);
       while(~scanf("%d",&n))
       {
          Initiate();
          for(int i=0;i<n-1;i++){
             scanf("%d%d%d",&ee[i].u,&ee[i].v,&ee[i].w);
          }
          sort(ee,ee+n-1,cmp);
          for(int i=0;i<n-1;i++){
             int r1=Find(ee[i].u);
             int r2=Find(ee[i].v);
             LL w=(LL)ee[i].w;
             LL tmp1=cnt[r2]*w+sum[r1];//方案的r1为根
             LL tmp2=cnt[r1]*w+sum[r2];
             if(tmp1>tmp2){
                Union(r2,r1,tmp1-sum[r1]);
             }else
                Union(r1,r2,tmp2-sum[r2]);//tmp2-sum[r2]为cnt[r1]*w,先说w为这个树里面最小的权值,那么w*cnt(节点数)肯定是这个树最大的权值和,
                                          //加入的下一个点则是选择以这个点为中心还是以这个树的某一个点中为中心(由新加的的点的边来决定),比较两种方案的总权值和。
                                          //此时若是以旧树的点为中的话,直接加cnt[新点]*w+旧树权值和,然后把新点合并旧树组成新树sum[这个树的根]继续更新。若是以新点
                                          //为中心,则是cnt[旧树根](cnt[旧树根]为旧树的节点)*w,(因为此时新加的w是最小的),加上sum[新点]。比较两种方案大小后,合并
                                          //确定是新点为新树的根(则此时是w*旧树节点树),sum[新点]更新,还是旧树根吸收新点。
    
          }
          printf("%I64d
    ",sum[Find(1)]);//随便输入这个树的一个节点
       }
       return 0;
    }
    


  • 相关阅读:
    生成随机端口函数
    于获得MFC窗口其它类指针的方法
    VC6.0中使用ADO操作Access数据库 (转)
    【原创】C++利用IXMLDOM解析XML文件。
    转帖:用MFC对话框做无闪烁图片重绘一一 程序设计: icemen
    C代码优化方案(转)
    【转】C++ Socket UDP "Hello World!"
    线程中使用UpdateData出错解决方法(转)
    C语言调试打印log函数。
    Windows Sockets 网络编程(三) —— WINDOWS SOCKETS 1.1 程序设计(转)
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246223.html
Copyright © 2011-2022 走看看