luogu#P9466. [EGOI 2023] Bikes vs Cars / 骑车与汽车
[EGOI 2023] Bikes vs Cars / 骑车与汽车
Background
Day 1 Problem D.
Translated from EGOI2023 bikesvscars.
Problem Description
In Lund, cycling is a common way to get around, but sometimes narrow streets make it hard for cars and bicycles to pass. To improve this situation, the local government wants to completely redesign the city’s street network.
There are key locations in Lund (numbered from to ), and people often travel between them. People travel between two locations along a path, where a path is a sequence of streets from one location to the other. A vehicle (car or bicycle) can travel along a path if and only if all streets on the path are at least as wide as the vehicle. Each newly built street connects two key locations and has total width . This width can be split in any way into a bicycle lane and a car lane. In Lund, some engineers have recently invented cars and bicycles of width (they can travel on roads of width ).
These engineers measured the widths of cars and bicycles in the city. For every pair of key locations, they know the width of the widest car and the widest bicycle that can travel between them, and the government also requires that wider cars and bicycles must not be able to travel between them.
Formally, for any (), you are given two integers and . Your task is to construct a street network connecting these locations. All streets have total width , but for any street , you may choose its bicycle-lane width and car-lane width . The network must satisfy:
- It must be possible to travel between any two locations. This may require a bicycle or car of width .
- For each pair of locations (where ), it must be possible to travel between and using only car lanes of width at least . Also, is the largest number with this property. This means that for every path between and , there must be at least one street whose car-lane width is at most .
- For each pair of locations (where ), it must be possible to travel between and using only bicycle lanes of width at least . Also, is the largest number with this property.
Can you help the Lund government design such a street network? Because the budget is limited, you may build at most streets. You may build multiple streets between the same pair of key locations, but you may not build a street from a location to itself. All streets are bidirectional.
Input Format
The first line contains two integers , the number of key locations and the street width.
The next lines contain . The -th of these lines contains all with . Therefore, the first line contains only , the second line contains and , the third line contains , and so on.
The next lines contain , in the same format as .
Output Format
If no street network satisfying the requirements exists, output NO.
Otherwise, output an integer on the first line, the number of streets you build.
Then output lines, each containing three integers , describing a street between and whose bicycle-lane width is (and car-lane width is ).
You may use at most streets. The streets you output must satisfy , , and . You may use multiple streets between the same pair of key locations (possibly with different bicycle-lane widths).
If there are multiple solutions, you may output any one.
2 1
1
1
2
0 1 0
0 1 1
4 1
0
0 1
0 0 1
1
1 1
1 1 1
NO
6 6
5
4 4
1 1 1
1 1 1 3
1 1 1 5 3
2
3 2
6 2 3
3 2 5 3
3 2 4 3 4
8
0 1 1
0 2 3
1 2 2
0 3 6
2 4 5
3 4 3
3 5 1
4 5 4
Hint
Explanation of Sample 1
In sample , the street width is . We need a bicycle lane of width at least and a car lane of width at least between and . The solution is to connect them with two separate streets: one with bicycle-lane width , and the other with car-lane width .

Explanation of Sample 2
In sample , the street width is still . We need a bicycle lane of width connecting every pair of key locations, and a car lane of width connecting and . This contradicts : there should not be a car-lane path of width from to , but we could combine the two paths mentioned above to form such a path. Therefore, it is impossible to construct a street network that satisfies the requirements.
Explanation of Sample 3
In sample , the street network in the figure satisfies all requirements. For example, there should be a car-lane path of width between and (path ), and a bicycle-lane path of width between and (path ). Also, one can verify that there is no path with a larger minimum width. Note that sample may have many other valid solutions.

Constraints
For all testdata, , , .
- Subtask 1 ( points): all are equal, all are equal, .
- Subtask 2 ( points): all are equal, all are equal.
- Subtask 3 ( points): .
- Subtask 4 ( points): .
- Subtask 5 ( points): all are equal.
- Subtask 6 ( points): no special constraints.
Translated by ChatGPT 5
