hdu 3660 Alice and Bob's Trip(树形DP)
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
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.
There is a blank line after each test case.
Proceed to the end of file.
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
2
6
2
题意:
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)的更多相关文章
- HDU 3660 Alice and Bob's Trip
树形dp,这道题如果选G++的话,只输入都会超时.我是C++ 1900ms + 飘过的...但是输入优化后就快了很多了,1100ms左右.dfs按层次求最值就行了,差不多也算是博弈吧,到bob取的时候 ...
- 【HDOJ】3660 Alice and Bob's Trip
就是一个基本的dfs.可关键问题是c/c++/g++光输入就超时了.还是写java过的,毕竟时限4s.都放弃希望了,没想到还真过了. import java.lang.*; import java.i ...
- HDU 4123 Bob’s Race 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 4268 Alice and Bob
Alice and Bob Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- hdu 4268 Alice and Bob(multiset|段树)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...
随机推荐
- [转]SPFA算法的玄学方法
最近想到了许多优化spfa的方法,这里想写个日报与大家探讨下 前置知识:spfa(不带任何优化) 由于使用较多 STLSTL ,本文中所有代码的评测均开启 O_2O2 优化 对一些数组的定义: di ...
- odoo12主题样式模块
odoo12主题样式模块 图标形式显示菜单目录的结构 odoo12图标形式显示菜单:https://apps.odoo.com/apps/themes/12.0/backend_theme_v12/ ...
- nexus-maven- repository-index.zip手动下载与设置
问题描述: 在启动eclipse的时候,在maven控制台经常会看到更新nexus-maven-repository-index.zip,用 eclipse更新速度会很慢,甚至有不能完成下载的情况: ...
- CENTOS下搭建SVN服务器(转)
1.安装svn yum install -y subversion 2.验证安装是否成功 svnserve --version 3.创建svn版本库 mkdir svn svnadmin create ...
- 领域Model?
前言 领域驱动设计里有很多东西,我们可以应用在各种各样的开发模式里,所以接下来说的一些东西,我们可以部分使用. 说道领域驱动的领域,大家肯定就要开始说Bounded Context,聚合,聚合根,容易 ...
- 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube
错误提示: 处理报表时出错. (rsProcessingAborted)对数据集“dsArea”执行查询失败. (rsErrorExecutingCommand)Team System 多维数据集或者 ...
- 一个简单例子理解C#的协变和逆变
关于协变逆变,SolidMango的解释是比较可取的.有了协变,比如,在需要返回IEnumerable<object>类型的时候,可以使用IEnmerable<string>来 ...
- springboot static方法与构造方法加载@VALUE
application.properties文件 mongodb.host=host111 mongodb.port=port222 import org.springframework.beans. ...
- ASIHTTPRequest学习笔记
1.creating requestsrequest分为同步和异步两种.不同之处在于开始request的函数:[request startSynchronous];[request startAsyn ...
- iOS中block简介-作用域
转:http://www.2cto.com/kf/201401/269467.html 用block可以定义任意的代码片段,将其像对象一样传入另一个方法:它是c级别的语法,和C语言中的函数指针非常相似 ...