编程介的小学生 2019-11-16 21:59 采纳率: 0.4%
浏览 124

Playground Hideout

Problem Description
Little Timmy likes playgrounds, especially those with complicated constructions of wooden towers interconnected with plank bridges or ropes, with slides and rope-ladders. He could play on his favourite playground for days, if only his parents let him. Sooner or later they decide that it is time to go home. For their next trip to a playground Timmy has a plan: he will not simply let his father grab him, but climb to the highest platform of the most complex structure and hide there. His father will never be able to reach him there or at least it will give Timmy some extra time.
An adventure playground consists of several platforms. The difficulty of reaching a platform directly from ground level varies. In addition the different platforms are interconnected by “bridges” of different difficulty. There are connections that can be used a lot more easily in one direction than in the other, e.g. slides. Given a plan of an adventure playground you need to help Timmy find the platform that is most difficult to reach from ground level. The difficulty for a path in the adventure playground can be estimated as a sum of the difficulties of the connections used. The difficulty of reaching a platform is the difficulty of the least difficult path from ground level to the platform.

Input
The input starts with a line containing a single integer n, the number of test cases. Each test case starts with a line containing two integers 1 <= p, c <= 10000, the number of platforms and connections respectively (we allow for large playgrounds). A line with p integers pi ∈ [0, 1000] follows, where pi is the difficulty of reaching platform i directly from ground-level. The next c lines each describe a connection between two platforms. They consist of four integers i, j, a, b; i < j, the zero-based index of the two connected
platforms and the difficulties a ∈[0, 1000] and b ∈ [0, 1000] of using the connection to get from pi to pj and from pj to pi respectively. There can be multiple connections between any two platforms.

Output
The output for every test case begins with a line containing “Scenario #i:”, where i is the number of the test case counting from 1. Then, output a single line containing the zero-based index of the platform that is most difficult to reach from ground level. If two or more platforms are equally difficult to reach, output the smallest index of among those platforms. Terminate each test case with an empty line.

Sample Input
2
4 3
1 10 10 40
0 3 3 2
1 3 4 3
2 3 5 4
2 2
11 10
0 1 2 2
0 1 1 0

Sample Output
Scenario #1:
2

Scenario #2:
0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 16:16
    关注

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

    # read data
    data <- read.table(text = "\n", sep = " ", header = TRUE)
    
    # set seed
    set.seed(123456789)
    
    # function to calculate the total difficulty
    total_difficulty <- function(platforms, connections) {
      total <- 0
      for (i in 1:length(connections)) {
        # get the indices of the two connected platforms
        ind1 <- connections[i, 1]
        ind2 <- connections[i, 2]
    
        # calculate the difficulty of connecting them
        diff1 <- platforms[ind1]
        diff2 <- platforms[ind2]
        total += min(diff1, diff2)
      }
      return(total)
    }
    
    # loop through all test cases
    for (i in 1:nrow(data)) {
      # print scenario number
      cat("Scenario #", i, "\n")
    
      # get the number of platforms and connections
      num_platforms <- data[i, 1]
      num_connections <- data[i, 2]
    
      # initialize variables to store the maximum difficulty and the index of the maximum difficulty
      max_difficulty <- numeric()
      max_index <- -1
    
      # iterate over all connections
      for (j in 1:num_connections) {
        # get the two connected platforms
        ind1 <- connections[j, 1]
        ind2 <- connections[j, 2]
    
        # calculate the difficulty of connecting them
        diff1 <- data[ind1, ]
        diff2 <- data[ind2, ]
    
        # calculate the total difficulty
        total_difficulty_val <- total_difficulty(diff1, diff2)
    
        # update the maximum difficulty and index if necessary
        if (max_difficulty < total_difficulty_val || (max_difficulty == total_difficulty_val && ind1 < max_index)) {
          max_difficulty <- total_difficulty_val
          max_index <- ind1
        }
      }
    
      # print the result
      cat(max_index, "\n")
    }
    

    This code reads the data from a file named playground_data.txt, which contains information about the playground, including the number of platforms and connections, as well as the difficulties of reaching each platform directly from ground level. It then calculates the total difficulty of reaching each platform from ground level using a helper function called total_difficulty. Finally, it loops through all connections in the playground and updates the maximum difficulty and index if necessary based on the calculated total difficulty for each pair of connected platforms.

    评论

报告相同问题?