UVA 1484 Alice and Bob's Trip (树形dp)
来源:互联网 发布:一个程序员的自我修养 编辑:程序博客网 时间:2024/05/01 10:57
题意:
Alice和Bob(又是这两个家伙)从树根出发,要走到叶子,Bob希望走长一点,Alice希望短一点,于是两个人轮流选取边去走,但是两个人选的边总和要在[L,R]之间。
题解:
dp[u][2],表示在根u时,0表示Bob选,1表示Alice选,能得到的最优解。
#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<queue>#include<map>#include<set>#define B(x) (1<<(x))using namespace std;typedef long long ll;void cmax(int& a,int b){ if(b>a)a=b; }void cmin(int& a,int b){ if(b<a)a=b; }void cmax(ll& a,ll b){ if(b>a)a=b; }void cmin(ll& a,ll b){ if(b<a)a=b; }void add(int& a,int b,int mod){ a=(a+b)%mod; }void add(ll& a,ll b,ll mod){ a=(a+b)%mod; }const int oo=0x3f3f3f3f;const ll OO=0x3f3f3f3f3f3f3f3f;const int MOD=100007;const int maxn=500005;const int maxm=10000005;struct EDGE{ int v,w,next;}E[maxn];int head[maxn],tol;int dp[maxn][2];int L,R;void Init(){ memset(head,-1,sizeof head); tol=0;}void add_edge(int u,int v,int w){ E[tol].v=v; E[tol].w=w; E[tol].next=head[u]; head[u]=tol++;}void tree_dp(int u,int up){ dp[u][0]=0; dp[u][1]=oo; int f=0; for(int i=head[u];i!=-1;i=E[i].next){ int v=E[i].v; int w=E[i].w; f=1; tree_dp(v,up+w); if(L<=dp[v][1]+w+up&&dp[v][1]+w+up<=R) cmax(dp[u][0],dp[v][1]+w); if(L<=dp[v][0]+w+up&&dp[v][0]+w+up<=R) cmin(dp[u][1],dp[v][0]+w); } if(!f) dp[u][0]=dp[u][1]=0;}int main(){ //freopen("E:\\read.txt","r",stdin); int n,u,v,w; while(scanf("%d %d %d",&n,&L,&R)!=EOF){ Init(); for(int i=1;i<=n-1;i++){ scanf("%d %d %d",&u,&v,&w); add_edge(u,v,w); } tree_dp(0,0); if(dp[0][0]<L||dp[0][0]>R) printf("Oh, my god!\n"); else printf("%d\n",dp[0][0]); } return 0;}
0 0
- uva 1484 Alice and Bob's Trip (树形dp)
- UVA 1484 - Alice and Bob's Trip(树形DP)
- uva 1484 - Alice and Bob's Trip(树形dp)
- UVA 1484 Alice and Bob's Trip (树形dp)
- Uva 1484 Alice and Bob's Trip(树形dp)
- uva-1484(hdu-3660) Alice and Bob's Trip 听说是树形dp
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- hdu - 3660 - Alice and Bob's Trip(树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- 树形dp-hdu-3660-Alice and Bob's Trip
- hdu 3660 Alice and Bob's Trip(树形DP)
- hdu3660 Alice and Bob's Trip 树形dp
- UVA1484 Alice and Bob's Trip(树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- uva1484 Alice and Bob's Trip (树形dp)
- hdoj 3660 Alice and Bob's Trip(树dp)
- HDU 3660 Alice and Bob's Trip
- hdoj 3660 Alice and Bob's Trip
- 决策树的剪枝理论
- 一些计算机相关的在线工具
- SpringMvc 上传文件优化
- vim常用命令
- 15、 老项目在Xcode6调试运行时报错的问题(App installation failed).
- UVA 1484 Alice and Bob's Trip (树形dp)
- 安装radaius用到的
- poj2932
- 配置免安装版MySQL
- 第3题
- 开博第一篇
- hdu1272 小希的迷宫
- VIM 使用技巧
- 基于无线ZigBee的污水监测系统