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. ARP监控工具ARPalert

    ARP监控工具ARPalert   ARP协议用于IP转化为Mac地址.由于ARP协议的缺陷,导致被用于中间人攻击.为了防止ARP攻击,Kali Linux提供了专用工具ARPalert.该工具可以对 ...

  2. zk节点扩充

    zk节点扩充,从3个节点扩充为7个节点,需要先安装4个节点,在将4个节点的配置进行修改,然后在修改 原有的3个节点.至此完成对zk的扩充实现,在此做个记录. zk节点的顺序,与对应zk与所在序列保持一 ...

  3. mount: unknown filesystem type 'vboxsf' centos ubuntu 处理方案

    Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem " ...

  4. POP3_关于 multipart/related;boundary=

    http协议对mime类型有详细描述,multipart/....是单个消息头包含多个消息体的解决方案.multipart媒体类型对发送非文本的各媒体类型是有用的.目前常用的有这些subtype: M ...

  5. 使用 Python 登录网站(转)

    对于大部分论坛,我们想要抓取其中的帖子分析,首先需要登录,否则无法查看. 这是因为 HTTP 协议是一个无状态(Stateless)的协议,服务器如何知道当前请求连接的用户是否已经登录了呢?有两种方式 ...

  6. HAL驱动的串口编程陷阱

    http://bbs.elecfans.com/jishu_464356_1_1.html 手上有块NUCLEO STM32L053x板子,用来做串口实验,看了下ST的最新库HAL驱动,于是想用HAL ...

  7. mysql 阿里内核人员

    丁奇 http://dinglin.javaeye.com/ 鸣嵩 @曹伟-鸣嵩 (新浪微博) 彭立勋 http://www.penglixun.com/ 皓庭 http://wqtn22.iteye ...

  8. .NET轻量级DBHelpers数据访问组件

    一.摘要 一说到ADO.NET大家可能立刻想到的就是增.删.改.查(CRUD)操作,然后再接就想到项目中的SQLHelper.没错本课分享课阿笨给大家带来的是来源于github上开源的DAO数据库访问 ...

  9. delphi GetKeyState

    GetKeyState(VK_CAPITAL) & 0x0001 0x8000 是键有否按下0x0001 是键的翻转状态 var bF1Down: Boolean;begin bF1Down ...

  10. 在delphi中嵌入脚本语言--(译)RemObjects Pascal Script使用说明(1)(译)

    翻譯這篇文章源於我的一個通用工資計算平台的想法,在工資的計算中,不可避免的需要使用到自定義公式,然而對於自定義公式的實現,我自己想了一些,也在網上搜索了很多,解決辦法大致有以下幾種: 1. 自己寫代碼 ...