【HDOJ】3660 Alice and Bob's Trip
就是一个基本的dfs。可关键问题是c/c++/g++光输入就超时了。还是写java过的,毕竟时限4s。都放弃希望了,没想到还真过了。
import java.lang.*;
import java.io.*;
import java.util.*; public class Main { public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in, out);
out.close();
}
} class TaskA {
public final static int maxv = (int)(5e5+5);
public final static int INF = 0x3f3f3f3f;
int[] V = new int[maxv];
int[] W = new int[maxv];
int[] nxt = new int[maxv];
int[] head = new int[maxv];
int m, n, L, R; void init() {
m = 0;
Arrays.fill(head, -1);
} void addEdge(int u, int v, int w) {
V[m] = v;
W[m] = w;
nxt[m] = head[u];
head[u] = m++;
} public void solve(InputReader in, PrintWriter out) {
int i;
int u, v, w;
int ans; while (true) {
try {
n = in.nextInt();
} catch (RuntimeException e) {
break;
}
L = in.nextInt();
R = in.nextInt();
init();
for (i=1; i<n; ++i) {
u = in.nextInt();
v = in.nextInt();
w = in.nextInt();
addEdge(u, v, w);
}
ans = dfs(0, 0, 0);
if (ans == INF)
out.println("Oh, my god!");
else
out.println(ans);
}
} private int dfs(int u, int len, int now) {
int ans = -1; if (head[u] == -1)
return 0;
int i, v, w; for (i=head[u]; i!=-1; i=nxt[i]) {
v = V[i];
w = W[i];
int tmp = dfs(v, len+w, now^1) + w;
if (tmp == INF)
continue;
if (tmp>=L-len && tmp<=R-len) {
if (now == 0) {
if (ans==-1 || tmp>ans)
ans = tmp;
} else {
if (ans==-1 || tmp<ans)
ans = tmp;
}
}
} if (ans == -1)
return INF;
return ans;
}
} class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
【HDOJ】3660 Alice and Bob's Trip的更多相关文章
- 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/Oth ...
- 【HDOJ】4122 Alice's mooncake shop
RMQ的基础题目,简单题. /* 4122 */ #include <iostream> #include <sstream> #include <string> ...
- 【HDOJ】3220 Alice’s Cube
状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...
- HDU 3660 Alice and Bob's Trip
树形dp,这道题如果选G++的话,只输入都会超时.我是C++ 1900ms + 飘过的...但是输入优化后就快了很多了,1100ms左右.dfs按层次求最值就行了,差不多也算是博弈吧,到bob取的时候 ...
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【博弈+GCD】C. Alice and Bob
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/C [题意] 初始时有n个数,定义操作为从n个数中取出两个数x,y,如果|x-y| ...
- UVA1484 Alice and Bob's Trip (hdu3660)
一.前言 最开始卡这题是某大佬给出的树DP专题中的一个,据说类似于对抗搜索(这是啥?)的一题 但是在经历了若干艰难困苦之后发现这题在HDU上A不了——(先卡vector的时间,后卡输入的时间,上了输入 ...
- 【dp】codeforces C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
随机推荐
- C# TcpClient 连接状态检测
C# TcpClient在连接成功后无法检测连接状态,即使对方关闭了网络连接.以下扩展可检测连接状态: public static class TcpClientEx { public static ...
- WCF编程系列(五)元数据
WCF编程系列(五)元数据 示例一中我们使用了scvutil命令自动生成了服务的客户端代理类: svcutil http://localhost:8000/?wsdl /o:FirstServic ...
- ###《Effective STL》--Chapter6
点击查看Evernote原文. #@author: gr #@date: 2014-09-27 #@email: forgerui@gmail.com Chapter6 函数子.函数子类.函数及其他 ...
- C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)
学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...
- JS代码大全 (都是网上看到 自己整理的)
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event. ...
- 九度OJ 1214 寻找丑数【算法】
题目地址:http://ac.jobdu.com/problem.php?pid=1214 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因 ...
- 怎么用程序获取远程url执行后的图片地址
远程URL:https://121.199.16.229:8890/generate.cgi?rbid=1001&esn=22021434025005&pic=png&coun ...
- WCF学习笔记(基于REST规则方式)
一.WCF的定义 WCF是.NET 3.0后开始引入的新技术,意为基于windows平台的通讯服务. 首先在学习WCF之前,我们也知道他其实是加强版的一个面向服务(SOA)的框架技术. 如果熟悉Web ...
- MySQL基础学习之数据库
创建一个新的数据库 create database 数据库名称; 查看所有数据库 show databases; 删除数据库 drop database 数据库名称
- 使用curl获取网站的http的状态码
发布:thebaby 来源:net [大 中 小] 本文分享一例shell脚本,一个使用curl命令获取网站的httpd状态码的例子,有需要的朋友参考下.本文转自:http://www.j ...