#P17141. [NOI 2026] 传送
[NOI 2026] 传送
Background
The statement and sample attachments come from QOJ.
When submitting to Luogu, there is no need to include the header #include "teleport.h". Just copy
std::vector<std::pair<long long, int>> teleport(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> x, std::vector<int> y);
to the beginning of your program, and compile with a C++17 or higher compiler.
Problem Description
Country has cities, numbered . These cities are connected by roads, forming a tree structure. The -th () road connects cities and , and it takes unit of time to travel from one endpoint city to the other.
To improve traffic efficiency, Country has developed a new type of teleport gate. Each city has one teleport gate. Using a teleport gate also takes unit of time, but since the system is not yet stable, it will teleport the user to one of all cities with equal probability. Note: using a teleport gate may also teleport you to the city you are currently in.
To test the effect of the teleport gates, Country conducted tests. In the -th () test, the tester is required to start from city and go to city . During the trip from the start to the destination, the tester may choose to move along roads or use teleport gates. Since there are many possible ways to travel, the tester needs to find the travel strategy with the minimum expected time.
Specifically, define a travel strategy as follows: for every non-destination city, choose one adjacent city or choose to use the teleport gate. Every time the tester arrives at that city, they will move according to the pre-determined choice, i.e., move to the chosen adjacent city, or use the teleport gate.
Formally, a travel strategy can be represented by a sequence of length , , where , and for all , is adjacent to , or . Every time the tester arrives at city (), if , the tester moves to ; otherwise, the tester uses the teleport gate.
A travel strategy is called valid if and only if its expected time is finite.
For each test, compute the minimum expected time among all valid travel strategies.
【Implementation Details】
Contestants do not need to, and should not, implement the main function.
Contestants need to ensure that the submitted source file includes the header teleport.h, i.e., add the following code at the beginning of the program:
#include "teleport.h"
Contestants need to implement the following function in the submitted source file teleport.cpp:
std::vector<std::pair<long long, int>> teleport(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> x, std::vector<int> y);
- represent the test point index, the number of cities, and the number of tests, respectively. means this test point is the sample.
- represent the two cities connected by each road.
- represent the start and the destination of each test.
- This function needs to return a sequence of pairs of length exactly : , where () mean that, in the -th test, the minimum expected time in lowest terms is . In particular, if the minimum expected time is a positive integer, treat it as .
- For each test point, this function will be called exactly once by the grader.
template_teleport.cpp in this problem directory is the provided sample code. You may refer to it and implement your own solution.
Input Format
【Grader Program Mode】
You can compile an executable in this problem directory with the following command:
g++ grader.cpp teleport.cpp -o teleport -O2 -std=c++14 -static
For the compiled executable teleport:
- The executable reads input from standard input in the following format:
- The first line contains three non-negative integers .
- Line () contains two non-negative integers .
- Line () contains two non-negative integers .
- The executable outputs to standard output in the following format:
- Line () contains two positive integers .
0 4 4
0 1
1 2
2 3
0 3
0 1
0 2
1 2
7 3
1 1
2 1
1 1
Hint
【Sample Explanation】
For the -th test:
- If the travel strategy is , then the time cost is the fixed value .
- If the travel strategy is , then the tester will keep using the teleport gate until leaving city , so the expected time is .
- If the travel strategy is , then the tester will keep using the teleport gate until reaching city or city , so the expected time is .
- If the travel strategy is , then the tester will move forever between city and city , so this travel strategy is not valid.
It can be proven that the minimum expected time is .
【Sample 】
See teleport/teleport2.in and teleport/teleport2.ans in the contestant directory.
This sample satisfies the constraints of test points .
【Sample 】
See teleport/teleport3.in and teleport/teleport3.ans in the contestant directory.
This sample satisfies the constraints of test points .
【Sample 】
See teleport/teleport4.in and teleport/teleport4.ans in the contestant directory.
This sample satisfies the constraints of test points .
【Sample 】
See teleport/teleport5.in and teleport/teleport5.ans in the contestant directory.
This sample satisfies the constraints of test point .
【Sample 】
See teleport/teleport6.in and teleport/teleport6.ans in the contestant directory.
This sample satisfies the constraints of test point .
【Sample 】
See teleport/teleport7.in and teleport/teleport7.ans in the contestant directory.
This sample satisfies the constraints of test points .
【Constraints】
For all testdata:
- , .
- For all , , and all form a tree.
- For all , and .
::cute-table{tuack} | Test Point Index | | | Special Property | |:-:|:-:|:-:|:-:| | | | | None | | | | | ^ | | | | | ^ | | | | | | | | ^ | | None | | | | ^ | | | | ^ | ^ | None | | | | | | | | ^ | | None |
Special property : For all , and .
Special property : For all , .
Translated by ChatGPT 5