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. 一个简单的日志函数C++

    有时候程序总是会发生意想不到的情况,为了方便排查错误的情况,还是写日志比较方便.这里自己写了一个简单的函数,能实现基本的功能. BOOL WriteLog(char * DataBuffer) { C ...

  2. Redis高可用之集群配置(六)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

  3. android 设置为系统应用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 将一个应用apk放到手机的 /系统/应用  这个目录下, 就会是 系统应用.

  4. luoguP3175 [HAOI2015]按位或 min-max容斥 + 高维前缀和

    考虑min-max容斥 \(E[max(S)] = \sum \limits_{T \subset S} min(T)\) \(min(T)\)是可以被表示出来 即所有与\(T\)有交集的数的概率的和 ...

  5. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

  6. Linux学习笔记02—磁盘分区

    下面介绍四种最常见的分区方式: (1)    最简单的分区方案. SWAP分区:即交换分区,建议大小是物理内存的1-2倍. /分区:建议大小在6GB以上. 使用以上的分区方案,所有的数据都在/分区上, ...

  7. linearLayout 和 relativeLayout的属性区别(转)

    LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件android:id="@+id/btn1" 控件宽度android:layo ...

  8. Revit API封装一个通用函数“过名称找元素”

    感觉这个函数不错.通过这种方式寻找元素经常需要用到. )         {  ];         }         // cannot find it.         return null; ...

  9. Snmp学习总结(三)——Win7安装和配置SNMP

    一.安装SNMP Win7操作系统默认情况下是不安装SNMP服务的,今天讲解一下在Win7操作系统下安装SNMP,具体安装步骤如下: WIN7操作系统下安装SNMP的步骤如下: 开始安装SNMP

  10. Android笔记之属性动画

    前言.动画分类 例如以下图所看到的,Android的动画主要分为三种: 以下首先说说 属性动画 所谓属性动画-- 就是指对象的属性值发生了变化,如控件位置和透明度等. 举例,如今要实现一个按键先下移. ...