zoukankan      html  css  js  c++  java
  • 2012年亚洲长春区域赛:E Conquer a New Region

    E - Conquer a New Region

    E - Conquer a New Region
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    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 capacityC(ij) of a road indicating it is allowed to transport at most C(ij) 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(ij) 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$ \le$N$ \le$200, 000)

    The next N - 1 lines each contains three integers abc indicating there is a road between town a and town b whose capacity is c. ( 1$ \le$ab$ \le$N1$ \le$c$ \le$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
    

     解题思路:因为一条路上的运输力是限制于路上最小的流量, 那麽边按流量大到小排序后,再进行处理。

    解题代码:

    View Code
     1 // File Name: E - Conquer a New Region
     2 // Author: sheng
     3 // Created Time: 2013年04月18日 星期四 10时31分37秒
     4 
     5 #include <iostream>
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include <algorithm>
     9 using namespace std;
    10 const int Maxn = 2e5+5;
    11 typedef long long LL;
    12 
    13 struct node
    14 {
    15     int x, y;
    16     int w;
    17     bool operator < (const node t1) const
    18     {
    19         return w > t1.w;
    20     }
    21 }town[Maxn];
    22 
    23 LL sum[Maxn];
    24 int cnt[Maxn];
    25 int fa[Maxn];
    26 
    27 int find(int x)
    28 {
    29     if (x != fa[x])
    30         return fa[x] = find(fa[x]);
    31     return x;
    32 }
    33 
    34 int main()
    35 {
    36     int i, n, a, b, c;
    37     while (scanf ("%d", &n) == 1)
    38     {
    39         for (i = 1; i < n; i ++)
    40             scanf ("%d%d%d", &town[i].x, &town[i].y, &town[i].w );
    41         for (i = 1; i <= n; i ++)
    42         {
    43             sum[i] = 0;
    44             cnt[i] = 1;
    45             fa[i] = i;
    46         }
    47         sort (town + 1, town + n);
    48         LL ans = 0;
    49         for (i = 1; i < n; i ++)
    50         {
    51             a = find (town[i].x);
    52             b = find (town[i].y);
    53             LL atob = (LL) cnt[a] * town[i].w + sum[b];
    54             LL btoa = (LL) cnt[b] * town[i].w + sum[a];
    55             if (atob > btoa )
    56             {
    57                 fa[a] = b;
    58                 cnt[b] += cnt[a];
    59                 sum[b] = atob;
    60             }
    61             else
    62             {
    63                 fa[b] = a;
    64                 cnt[a] += cnt[b];
    65                 sum[a] = btoa;
    66             }
    67             ans = max (ans, max(atob, btoa));
    68         }
    69         printf ("%lld\n", ans);
    70     }
    71 }
     
  • 相关阅读:
    剑指offer4:重建二叉树(后序遍历)
    剑指offer3:从尾到头打印链表每个节点的值
    剑指offer2:C++实现的替换空格(字符中的空格替换为“%20”)
    tp5系统变量输出(可以用来传递搜索的参数)
    Ajax实现文件上传的临时垃圾文件回收策略
    php获取当天的开始时间和结束时间
    Think PHP递归获取所有的子分类的ID (删除当前及子分类)
    tp查找某字段,排除某字段,不用一次写那么多
    git-查看历史版本及回滚版本
    dedecms目录结构,非常全
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3028240.html
Copyright © 2011-2022 走看看