【樹形DP—未解決】hdu 3660

來源:互聯網
上載者:User

題意:給出n個點,n-1條邊,問從節點0到最遠的子葉的距離滿足L<=dis<=R,且Bob總是選最大的,Alice總是選最小的,問最後能否滿足要求。

方法:樹形DP,dis[u]數組表示0節點到當前節點u的距離,dp[u]數組表示最遠的子分葉節點到u節點的滿足要求的距離,最後dp[0]就是答案。這dp妙就妙在利用了的dfs回溯,從最遠子節點到0節點進行dp

貼一個TLE代碼,有空a了他!

#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;#define LL long long#define PI acos(-1.0)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-8#define FRE freopen("a.txt","r",stdin)#define MOD 1000000007#define N 500010int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a>b?b:a;}struct edge{    int to;    int val;};vector<edge> v[N];int dis[N],dp[N];int l,r;void dfs(int u,int who){    int i,j;    if(v[u].size()==0){dp[u]=0;return ;}    dp[u]= who?0:MAX;    if(dis[u]>r)return ;    for(i=0;i<v[u].size();i++){        int to=v[u][i].to;        int w=v[u][i].val;        dis[to]=dis[u]+w;        dfs(to,!who);        if(dis[u]+w+dp[to]<=r && dis[u]+w+dp[to]>=l){            if(who)            dp[u]=max(dp[u],dp[to]+w);            else            dp[u]=min(dp[u],dp[to]+w);        }    }}int main(){    int n;    while(~scanf("%d%d%d",&n,&l,&r)){        int i,j,k;        for(i=0;i<=n;i++)v[i].clear();        for(i=1;i<n;i++){            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            edge tmp;            tmp.to=b;            tmp.val=c;            v[a].push_back(tmp);        }        dis[0]=0;        dfs(0,1);        if(dp[0]<=r && dp[0]>=l)printf("%d\n",dp[0]);        else        printf("Oh, my god!\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.