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 }
  • 相关阅读:
    函数模板、函数模板特化、重载函数模板、非模板函数重载
    输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
    文件的读写、二进制文件的读写、文件随机读写
    文件流(fstream, ifstream, ofstream)的打开关闭、流状态
    流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作
    对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
    operator new 和 operator delete 实现一个简单内存泄漏跟踪器
    异常与继承、异常与指针、异常规格说明
    程序错误、异常(语法、抛出、捕获、传播)、栈展开
    C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4312158.html
Copyright © 2011-2022 走看看