编程介的小学生 2020-02-03 21:53 采纳率: 0.4%
浏览 140

Alice and Bob's Trip

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

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 04:49
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # read input
    input <- readLines()
    
    # parse input
    n <- as.integer(input[1])
    l <- as.integer(input[2])
    r <- as.integer(input[3])
    
    edges <- list()
    for(i in 4:n){
      a <- as.integer(input[i][1]) - 1
      b <- as.integer(input[i][2]) - 1
      c <- as.integer(input[i][3])
      edges[[i]] <- list(a = a, b = b, c = c)
    }
    
    # calculate maximum possible value for Bob
    max_value_bob <- max(edges) + l
    
    if(max_value_bob < l || max_value_bob > r){
      print("Oh, my god!")
    } else {
      print(max_value_bob)
    }
    

    This code reads the input data from stdin and processes it accordingly. It calculates the maximum possible value for Bob and prints it out if it falls outside the specified range or otherwise prints "Oh, my god!".

    评论

报告相同问题?