zoukankan      html  css  js  c++  java
  • zoj3659 Conquer a New Region

    Conquer a New Region

    Time Limit: 5 Seconds Memory Limit: 32768 KB

    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
    这题用并查集来做,确实,很好啊,当时一看题,就有一种网络流的感觉,一直坑在那,其实,我们可以发现,这题是一个树,那么两点之间只能有一条路,也就是这一个重要性质,我们就可以用并查集,我们从大到小排一下序,对于两个集合a,b我们有两种合并方式,我们只要比较是a到b,还是b到a大,我们只要以那种最大的方式合并,就可以得到最大值了!因为,两点之间,只有一条边啊!
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    #define MAXN 200050
    struct node {
        int s,e,c;
        bool operator <(node a)const{return c>a.c;}
    }edge[MAXN];
    long long sum[MAXN];
    int no[MAXN],father[MAXN];
    bool cmp(node a,node b)
    {
        return a<b;
    }
    int find(int x)
    {
        if(father[x]!=x)
            father[x]=find(father[x]);
        return father[x];
    }
    int main()
    {
       int n,i;
       long long  tempa,tempb;
       while(scanf("%d",&n)!=EOF)
       {
           for(i=0;i<=n;i++)
           {
               no[i]=1,sum[i]=0,father[i]=i;
           }
           for(i=1;i<=n-1;i++)
           {
              scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].c);
           }
           sort(edge+1,edge+n,cmp);
           for(i=1;i<=n-1;i++)
           {
               int a=find(edge[i].s);
               int b=find(edge[i].e);
               if(a!=b)
               {
                   tempa=(long long )edge[i].c*no[b]+sum[a];
                   tempb=(long long )edge[i].c*no[a]+sum[b];
                   if(tempa>tempb)
                   {
                       father[b]=a,sum[a]=tempa,no[a]+=no[b];
                   }
                   else
                   {
                       father[a]=b,sum[b]=tempb,no[b]+=no[a];
                   }
               }
            }
            int a=find(1);
            printf("%lld
    ",sum[a]);
       }
        return 0;
    }
    
  • 相关阅读:
    初步学习vue.js
    ie兼容CSS3渐变写法
    关于javascript dom扩展:Selector API
    jquery 性能优化高级技巧
    三行代码CSS竖向居中
    JS中 (function(){...})()立即执行函数
    Null 和 undefined 的区别
    CSS 交集选择器和并集选择器
    Vue-cli 构建项目后 npm run build 如何在本地运行查看
    Cubic-bezier 曲线
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3279792.html
Copyright © 2011-2022 走看看