HDu 3586(ツリーdp)


Information Disturbing
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2351    Accepted Submission(s): 845
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

标题:1本の木に辺権があると言って、いくつかの辺を削除して、叶が木の根1に届かないようにして、しかも辺の権とm以下を削除して、削除辺の中で最も大きい権の最小値はいくらですかを闻きます.
考え方:
dp[i][j]は、iノードがルートであるサブツリーにおいて、その葉をルートに到達させない場合、削除エッジの最大重みはjの最小削除エッジ重み和に等しいものより小さいことを示す.
この問題で生まれて初めてINFの設定が大きすぎてwaが数回もない!!!主にdp初期化で全設定のINFが初期化されているので、tem配列が積算されるとintが爆発します....本当に心が疲れます....他の人のブログコードとほぼ同じに変更された結果、私のは間違っていました....
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=1100000;
struct data
{
    int to,w;
};
vector<data>tu[1111];
int m,n;
int dp[1111][1010];
int ml;
void dfs(int now,int w)
{
    if(now!=1)
        for(int i=w; i<=ml; i++)
            dp[now][i]=w;
    int l=tu[now].size();
    int tem[1010]= {0};
    for(int i=0; i<l; i++)
    {
        int to=tu[now][i].to,w=tu[now][i].w;
        dfs(to,w);
        for(int j=1; j<=ml; j++)
            tem[j]+=dp[to][j];
    }
    for(int i=1; i<=ml; i++)
        if(tem[i])
            dp[now][i]=min(dp[now][i],tem[i]);
}

int main()
{
    while(~scanf("%d%d",&n,&m)&&m+n)
    {
        for(int i=0; i<=n; i++)
            tu[i].clear();
        ml=0;
        for(int i=0; i<n-1; i++)
        {
            int a,b,w;
            data hh;
            scanf("%d%d%d",&a,&b,&w);
            ml=max(ml,w);
            hh.to=b,hh.w=w;
            tu[a].push_back(hh);
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=ml; j++)
                dp[i][j]=INF;
        dfs(1,0);
        int ans=-1;
        for(int i=1; i<=ml; i++)
            if(dp[1][i]<=m)
            {
                ans=i;
                break;
            }
        printf("%d
",ans); } return 0; }