1. 程式人生 > >HDU 3586.Information Disturbing 樹形dp

HDU 3586.Information Disturbing 樹形dp

form rec front mode pla case lin sin bat

Information Disturbing

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


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 2010 ACM-ICPC Multi-University Training Contest(15)——Host by NUDT

Recommend zhouzeyong | We have carefully selected several similar problems for you: 3583 3584 3585 3587 3588 題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 題意:有一棵n個節點的樹,現在需要刪除一些邊使得葉子節點和根(1)不是聯通的,刪除的總邊權不超過m,並且最大邊權最小。 思路:dp[u]表示u的子節點與u不相連的代價。<u,v>w,dp[u]+=min(dp[v],w)。當最大邊權為x時,需要進行判斷<u,v>w,如果w比x大的話,只能dp[u]+=dp[v]。所以二分最大邊權。 代碼: 技術分享
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define abs(a) (a)<0?-(a):(a)
typedef long long ll;
typedef pair<int,int > P;
const int N=2e5+100,M=2e6+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
struct edge
{
    int from,to;
    ll w;
    int next;
};
edge es[M];
int cnt,head[N];
ll dp[N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,ll w)
{
    cnt++;
    es[cnt].from=u,es[cnt].to=v;
    es[cnt].w=w;
    es[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u,int fa,ll mid)
{
    dp[u]=0LL;
    int child=0;
    for(int i=head[u]; i!=-1; i=es[i].next)
    {
        edge e=es[i];
        if(e.to==fa) continue;
        child++;
        dfs(e.to,u,mid);
        if(e.w>mid) dp[u]+=dp[e.to];
        else dp[u]+=min(dp[e.to],e.w);
    }
    if(!child) dp[u]=inf;
}
int main()
{
    int n;
    ll m;
    while(~scanf("%d%lld",&n,&m))
    {
        if(n==0&&m==0) break;
        init();
        ll l=inf,r=-inf;
        for(int i=1; i<n; i++)
        {
            int u,v;
            ll w;
            scanf("%d%d%lld",&u,&v,&w);
            l=min(l,w),r=max(r,w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        ll ans=0;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            dfs(1,0,mid);
            if(dp[1]<=m) r=mid-1,ans=mid;
            else l=mid+1;
        }
        if(ans) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}
樹形dp

HDU 3586.Information Disturbing 樹形dp