【HDOJ】3660 Alice and Bob's Trip

就是一个基本的dfs。可关键问题是c/c++/g++光输入就超时了。还是写java过的,毕竟时限4s。都放弃希望了,没想到还真过了。

  1 import java.lang.*;
  2 import java.io.*;
  3 import java.util.*;
  4
  5
  6 public class Main {
  7
  8     public static void main(String[] args) throws java.lang.Exception {
  9         InputStream inputStream = System.in;
 10         OutputStream outputStream = System.out;
 11         InputReader in = new InputReader(inputStream);
 12         PrintWriter out = new PrintWriter(outputStream);
 13         TaskA solver = new TaskA();
 14         solver.solve(in, out);
 15         out.close();
 16     }
 17 }
 18
 19 class TaskA {
 20     public final static int maxv = (int)(5e5+5);
 21     public final static int INF = 0x3f3f3f3f;
 22     int[] V = new int[maxv];
 23     int[] W = new int[maxv];
 24     int[] nxt = new int[maxv];
 25     int[] head = new int[maxv];
 26     int m, n, L, R;
 27
 28     void init() {
 29         m = 0;
 30         Arrays.fill(head, -1);
 31     }
 32
 33     void addEdge(int u, int v, int w) {
 34         V[m] = v;
 35         W[m] = w;
 36         nxt[m] = head[u];
 37         head[u] = m++;
 38     }
 39
 40     public void solve(InputReader in, PrintWriter out) {
 41         int i;
 42         int u, v, w;
 43         int ans;
 44
 45         while (true) {
 46             try {
 47                 n = in.nextInt();
 48             } catch (RuntimeException e) {
 49                 break;
 50             }
 51             L = in.nextInt();
 52             R = in.nextInt();
 53             init();
 54             for (i=1; i<n; ++i) {
 55                 u = in.nextInt();
 56                 v = in.nextInt();
 57                 w = in.nextInt();
 58                 addEdge(u, v, w);
 59             }
 60             ans = dfs(0, 0, 0);
 61             if (ans == INF)
 62                 out.println("Oh, my god!");
 63             else
 64                 out.println(ans);
 65         }
 66     }
 67
 68     private int dfs(int u, int len, int now) {
 69         int ans = -1;
 70
 71         if (head[u] == -1)
 72             return 0;
 73         int i, v, w;
 74
 75         for (i=head[u]; i!=-1; i=nxt[i]) {
 76             v = V[i];
 77             w = W[i];
 78             int tmp = dfs(v, len+w, now^1) + w;
 79             if (tmp == INF)
 80                 continue;
 81             if (tmp>=L-len && tmp<=R-len) {
 82                 if (now == 0) {
 83                     if (ans==-1 || tmp>ans)
 84                         ans = tmp;
 85                 } else {
 86                     if (ans==-1 || tmp<ans)
 87                         ans = tmp;
 88                 }
 89             }
 90         }
 91
 92         if (ans == -1)
 93             return INF;
 94         return ans;
 95     }
 96 }
 97
 98 class InputReader {
 99     public BufferedReader reader;
100     public StringTokenizer tokenizer;
101
102     public InputReader(InputStream stream) {
103         reader = new BufferedReader(new InputStreamReader(stream), 32768);
104         tokenizer = null;
105     }
106
107     public String next() {
108         while (tokenizer==null || !tokenizer.hasMoreTokens()) {
109             try {
110                 tokenizer = new StringTokenizer(reader.readLine());
111             } catch (IOException e) {
112                 throw new RuntimeException(e);
113             }
114         }
115         return tokenizer.nextToken();
116     }
117
118     public int nextInt() {
119         return Integer.parseInt(next());
120     }
121 }

【HDOJ】3660 Alice and Bob's Trip

时间: 2024-10-08 16:30:17

【HDOJ】3660 Alice and Bob's Trip的相关文章

【HDOJ】4122 Alice&#39;s mooncake shop

RMQ的基础题目,简单题. 1 /* 4122 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque>

【HDOJ】3220 Alice’s Cube

状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. 1 /* 3220 */ 2 #include <iostream> 3 #include <queue> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 using namespace std; 8 9 10 char

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)