【HDOJ】3660 Alice and Bob's Trip
2015-12-31 00:03
351 查看
就是一个基本的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()); } }
相关文章推荐
- WordPress中获取所使用的模板的页面ID的简单方法
- 简单了解将WordPress中的工具栏移到底部的小技巧
- WordPress中给媒体文件添加分类和标签的PHP功能实现
- Swoole-1.7.22 版本已发布,修复PHP7相关问题
- WordPress中获取指定分类及其子分类下的文章数目
- WordPress中登陆后关闭登陆页面及设置用户不可见栏目
- python实现爬虫统计学校BBS男女比例(一)
- python实现爬虫统计学校BBS男女比例之多线程爬虫(二)
- python实现爬虫统计学校BBS男女比例之数据处理(三)
- 基于python的Tkinter实现一个简易计算器
- python实现颜色空间转换程序(Tkinter)
- 简单实现python爬虫功能
- jQuery实现简单的图片查看器
- 简单谈谈JavaScript的同步与异步
- 解决JS无法调用Controller问题的方法
- jQuery语法小结(超实用)
- jQuery动画效果相关方法实例分析
- 详解javascript高级定时器
- jQuery动画显示和隐藏效果实例演示(附demo源码下载)
- javascript图片延迟加载实现方法及思路