首页 > ACM题库 > HDU-杭电 > HDU 3586- Information Disturbing-动态规划-[解题报告]HOJ
2014
11-27

HDU 3586- Information Disturbing-动态规划-[解题报告]HOJ

Information Disturbing

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0

样例输出:

3

题意:

给出一棵树,和每条边的cost值,设置一个界限,要求切断所有叶子结点,切断的边的cost不能超过界限值,并且切断的边的总和不能超过m,求出这个最小界限值。

解题思路:

很明显的一道树型DP,DP[v],表示切断根结点的v的子树的最小花费,那么这个界限怎么处理呢,由于界限值范围要比总和m小得多,才1-1000,可以用二分枚举,这样在选择切边时可以根据这个枚举值进行判断处理。每个结点v,设其儿子结点为x1,x2,x3…则DP[v] = Min{DP[x1],Node[x1].cost} + Min{DP[x2], Node[x2].cost} + …,当然,要判断Node[x].cost是否小于枚举的界限,Node[x].cost表示边(v,x)的cost值。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<memory.h>
using namespace std;
int dp[1002],n,m,NE,mid;
int head[1002],vis[1002];
const int INF=1000001;
struct node
{
    int u,v,val,next;
} Edge[2002];
void addEdge(int u,int v,int w)
{
    Edge[NE].u=u,Edge[NE].v=v,Edge[NE].next=head[u];
    Edge[NE].val=w;
    head[u]=NE++;
}
int dfs(int u,int fa)
{
    int i,sum=0;
    vis[u]=1;
    for(i=head[u]; i!=-1; i=Edge[i].next)
    {
        int r=Edge[i].v;
        if(fa==r) continue;
        int rr=dfs(r,u);
        if(rr>Edge[i].val&&Edge[i].val<=mid)
        {
            rr=Edge[i].val;
        }
        sum+=rr;
    }
    if(!sum)return INF;
    return sum;
}
int main()
{
    while(cin>>n>>m,(n||m))
    {
        NE=0;
        int i,j,k,a,b,w,ans=INF;
        int l=1002,r=0;
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        for(i=1; i<n; i++)
        {
            cin>>a>>b>>w;
            addEdge(a,b,w);
            addEdge(b,a,w);
            l=min(l,w);
            r=max(r,w);
        }
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(dfs(1,0)<=m)
            {
                ans=min(ans,mid);
                r=mid-1;
            }
            else l=mid+1;
        }
        if(ans>=INF)cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

参考:http://blog.csdn.net/azheng51714/article/details/7795962


  1. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }

  2. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks