Alice and Bob's Trip

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2643    Accepted Submission(s): 708

Problem Description
Alice and Bob are going on a trip. Alice is a lazy girl who wants to minimize the total travelling distance, while Bob as an active boy wants to maximize it. At the same time, they cannot let the value to be less than a given integer L since that will make them miss too much pleasure, and they cannot let the value to be greater than a given integer R since they don't want to get too exhausted.

The city they are visiting has n spots and the spots are connected by directed edges. The spots are connected in such a way that they form a tree and the root will always be at spot 0. They take turns to select which edge to go. Both of them choose optimally. Bob will go first.

 
Input
There are multiple test cases. For every test case, the first line has three integers, n, L and R (1<=n<=500000, 0<=L, R<=1000000000). The next n-1 lines each has three integers a, b and c, indicating that there is an edge going from spot a to spot b with length c (1<=c<=1000). The spots are labeled from 0 to n-1.

There is a blank line after each test case.

Proceed to the end of file.

 
Output
If the total distance is not within the range [L, R], print "Oh, my god!" on a single line. Otherwise, print the most value Bob can get.
 
Sample Input
3 2 4
0 1 1
0 2 5

7 2 8
0 1 1
0 2 1
1 3 1
1 4 10
2 5 1
2 6 5

7 4 8
0 1 1
0 2 1
1 3 1
1 4 2
2 5 1
2 6 5

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

 
Sample Output
Oh, my god!
2
6
2
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  
3669 
3668 
3667 
3666 
3662 
 

题意:

A和B要去旅游。这有N个城市。而这些的城市的道路为一颗树。且边有向。A和B从0出发一起去旅游。A很懒想尽量少走路。B很有活力想尽量多走路但是无论怎么选择他们走的总路程都必须满足在[L,R]的范围内。所以他们交替选择走哪条路。开始由B选。然后问你在都采用最优策略的情况下。B最多能走多少路。

思路:

算是比较水的树形DP吧。由于道路是一颗树所以很多东西是可以确定的。比如根到这个节点的距离。和该结点由谁选择。所以就很简单了。用dp[i]表示经过i号结点B所能获得的最大价值。为-1时表示不能经过该结点。比赛时太困了大脑完全就不转了。还好队友解决了。下来一下下就解决了。不过比赛时要优化输入才过不然要TLE。在外面用C++交就不会超时了。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=500010;
int cnt,n,L,R;
int dp[maxn];
struct node
{
int v;
int val;
node *next;
} edge[maxn],*head[maxn];
void adde(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].val=w;
edge[cnt].next=head[u];
head[u]=&edge[cnt++];
}
void dfs(int np,int d,bool ok)//ok=1表示B选0表示A选
{
node *p;
if(head[np]==NULL)//到叶子结点时判断可行性。
{
if(d>=L&&d<=R)
dp[np]=d;
else
dp[np]=-1;
return ;
}
for(p=head[np];p!=NULL;p=p->next)//只有先处理完儿子才能确定自己
dfs(p->v,d+p->val,ok^1);
if(ok)//初始化
dp[np]=-1;
else
dp[np]=INF;
for(p=head[np];p!=NULL;p=p->next)
{
if(ok)
dp[np]=max(dp[np],dp[p->v]);
else if(dp[p->v]!=-1)
dp[np]=min(dp[np],dp[p->v]);
}
if(dp[np]==INF)
dp[np]=-1;
//printf("np %d %d\n",np,dp[np]);
}
inline int getint()//优化输入
{
char ch=getchar();
int ans= 0;
while(ch<'0'||ch>'9')
ch=getchar();
do
{
ans=ans*10+ch-'0';
ch=getchar();
}while(ch>='0'&&ch<='9');
return ans;
}
int main()
{
int i,u,v,w; while(~scanf("%d%d%d",&n,&L,&R))
{
cnt=0;
memset(head,0,sizeof head);
for(i=1;i<n;i++)
{
//scanf("%d%d%d",&u,&v,&w);//优化前1765ms。优化后781ms。所以大型输入时最好还是优化下
u=getint();
v=getint();
w=getint();
adde(u,v,w);
}
dfs(0,0,1);
if(dp[0]>=0)
printf("%d\n",dp[0]);
else
printf("Oh, my god!\n");
}
return 0;
}

hdu 3660 Alice and Bob's Trip(树形DP)的更多相关文章

  1. HDU 3660 Alice and Bob's Trip

    树形dp,这道题如果选G++的话,只输入都会超时.我是C++ 1900ms + 飘过的...但是输入优化后就快了很多了,1100ms左右.dfs按层次求最值就行了,差不多也算是博弈吧,到bob取的时候 ...

  2. 【HDOJ】3660 Alice and Bob's Trip

    就是一个基本的dfs.可关键问题是c/c++/g++光输入就超时了.还是写java过的,毕竟时限4s.都放弃希望了,没想到还真过了. import java.lang.*; import java.i ...

  3. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  4. hdu 4111 Alice and Bob 记忆化搜索 博弈论

    Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  5. hdu 4268 Alice and Bob

    Alice and Bob Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  6. hdu 4268 Alice and Bob(multiset|段树)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  8. HDU 4268 Alice and Bob(贪心+Multiset的应用)

     题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...

  9. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

随机推荐

  1. bzoj5068: 友好的生物

    题目链接 bzoj5068: 友好的生物 题解 最大化这个东西\(\sum_{i=1}^{k-1} | a_{x,i}-a_{y,i} | - | a_{x,k}-a_{y,k} |\) 去掉绝对值号 ...

  2. HDU.5819.Knights(概率DP)

    题目链接 参考一下这的. \(Description\) 数轴上有n个骑士,分别位于1,2,3,...,n,它们的移动速度相同,初始移动方向已知.当两个骑士相遇时,各有50%的概率获胜,失败的骑士就死 ...

  3. BZOJ.3004.[SDOI2012]吊灯(结论)

    题目链接 BZOJ 洛谷 题意: 将树划分为k个连通块,要求每个连通块大小相同.输出可能的大小. 结论: 满足条件时颜色的连通块数为k,当且仅当有 \(n/k\) 个节点满足它的子树是k的倍数(显然还 ...

  4. [JZOJ4786]小a的强迫症

    [JZOJ4786]小a的强迫症 题目大意: 有\(n(n\le10^5)\)种颜色的珠子,第\(i\)种颜色有\(num[i]\)个.你要把这些珠子排成一排,使得第\(i\)种颜色的最后一个珠子一定 ...

  5. codeforces 596E Wilbur and Strings

    题意:一个矩阵上面有0~9的数字,可以从任意一个格子出发,每次根据格子上的数字会前进到另一个格子(或原地不动),现在给出q个数位串,问是否有走法可以取出这个串(走到格子上的时候可以不取). 思路:发现 ...

  6. 历数PC发展史上的祖先们

    转自泡泡网:http://www.pcpop.com/doc/0/774/774178_all.shtml 本文导航 第01页:这几十亿晶体管的前辈是? 第02页:编辑发明的QWERTY键盘 第03页 ...

  7. consul vs etcd3

    https://sysadmin.libhunt.com/project/etcd/vs/consul

  8. SourceTree的简单使用

    原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...

  9. delphi:临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 TRtlCriticalSection 是一个结构体,在windows单元中定义: 是I ...

  10. MySQL主从复制的原理及配置方法(比较详细)

    MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题 一.复制的原理 MySQL 复制基于主服务 ...