zoukankan      html  css  js  c++  java
  • HDU 3586 树状dp

    Information Disturbing

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 1668    Accepted Submission(s): 618


    Problem Description
    In the battlefield , an effective way to defeat enemies is to break their communication system.
    The information department told you that there are n enemy soldiers and their network which have n-1 communication routes can cover all of their soldiers. Information can exchange between any two soldiers by the communication routes. The number 1 soldier is the total commander and other soldiers who have only one neighbour is the frontline soldier.
    Your boss zzn ordered you to cut off some routes to make any frontline soldiers in the network cannot reflect the information they collect from the battlefield to the total commander( number 1 soldier).
    There is a kind of device who can choose some routes to cut off . But the cost (w) of any route you choose to cut off can’t be more than the device’s upper limit power. And the sum of the cost can’t be more than the device’s life m.
    Now please minimize the upper limit power of your device to finish your task.
     
    Input
    The input consists of several test cases.
    The first line of each test case contains 2 integers: n(n<=1000)m(m<=1000000).
    Each of the following N-1 lines is of the form:
    ai bi wi
    It means there’s one route from ai to bi(undirected) and it takes wi cost to cut off the route with the device.
    (1<=ai,bi<=n,1<=wi<=1000)
    The input ends with n=m=0.
     
    Output
    Each case should output one integer, the minimal possible upper limit power of your device to finish your task.
    If there is no way to finish the task, output -1.
     
    Sample Input
    5 5 1 3 2 1 4 3 3 5 5 4 2 6 0 0
     
    Sample Output
    3
     
    Author
    alpc86
     
    Source

    HDU 3586:

    题目意思;

    打断一棵树的叶子节点和根节点之间的联系,要求只有士兵的能力大于这条边所需要的能力时才能被打断,同时满足所有消耗的能力值之和不大于m,求士兵所具有的最小的能力值;


    解题思路:

    首先对士兵的能力值在一个区间内进行二分,然后对于每一个值进行dp,状态转移方程为:

    if(dp[son]>cost[father][son]&&cost[father][cost]<=mid)

    dp[son]=cost[father][son];

    然后求到根节点的时候,把根节点的儿子节点的所有dp值都加起来如果小于等于m,则返回真值,否则,返回0

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<vector>
     7 using namespace std;
     8 const  int maxn=2010;
     9 const int inf=1000001;
    10 vector<int> G[maxn];
    11 int cost[maxn][maxn];
    12 int n,m,head,mid;
    13 int dfs(int parent,int point)
    14 {
    15     int ans=0;
    16     for(int i=0;i<G[point].size();i++)
    17     {
    18          if(G[point][i]==parent) continue;
    19          int result=dfs(point,G[point][i]);
    20          if(cost[point][G[point][i]]<=result&&cost[point][G[point][i]]<=mid)
    21          {
    22              result=cost[point][G[point][i]];
    23          }
    24          ans+=result;
    25     }
    26     if(!ans) return inf;
    27     return ans;
    28 }
    29 int main()
    30 {
    31  //  freopen("in.txt","r",stdin);
    32     int u,v,c;
    33     while(~scanf("%d%d",&n,&m)){
    34         if(!n&&!m) break;
    35         int l=inf,r=0;
    36         for(int i=0;i<maxn;i++) while(!G[i].empty()) G[i].pop_back();
    37         for(int i=0;i<n-1;i++)
    38         {
    39             scanf("%d%d%d",&u,&v,&c);
    40             if(c<l) l=c;
    41             if(c>r) r=c;
    42             G[u].push_back(v);
    43             G[v].push_back(u);
    44         cost[u][v]=cost[v][u]=c;
    45         }
    46         int ans=-1;
    47         while(l<=r){
    48             mid=(l+r)>>1;
    49             if(dfs(0,1)<=m){
    50                 ans=mid;
    51                 r=mid-1;
    52             }
    53             else l=mid+1;
    54         }
    55          printf("%d
    ",ans);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    支持向量机(SVM)相关免费学习视频集锦
    《量化投资:以MATLAB为工具》连载(2)基础篇-N分钟学会MATLAB(中)
    《量化投资:以MATLAB为工具》连载(1)基础篇-N分钟学会MATLAB(上)
    OpenCV 轮廓基本特征
    !!破解灯塔线取点与画线的“难点”
    理工科应该的知道的C/C++数学计算库(转)
    521. 最长特殊序列 Ⅰ
    520. 检测大写字母
    459. 重复的子字符串
    443. 压缩字符串
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4312158.html
Copyright © 2011-2022 走看看