bikesvscars — EGOI contest
Task type: batch | Time limit: 5 s | Memory limit: 1024 MB | Difficulty: 21
Algorithms: shortest path, floyd-warshall, graph theory, spanning tree

![](_page_0_Figure_0.jpeg)

# Bikes vs Cars

| Problem Name | Bikes vs Cars |
|--------------|---------------|
| Time Limit   | 5 seconds     |
| Memory Limit | 1 gigabyte    |

In Lund, biking is a very common method of transportation, but it is sometimes difficult to fit both cars and cyclists on the narrow streets. To improve the situation, the local governor wants to completely redesign the local street network.

There are *N* important locations (numbered from 0 to *N* − 1) in Lund that people frequently travel between. People travel between two locations by following a path, which is a sequence of streets going from the first location to the other. A vehicle (car or bike) can travel on a path if all relevant lanes are at least as wide as the vehicle. Each newly built street connects two of these important locations and it has a total width of *W*. This width can be arbitrarily partitioned between bike lane and car lane. In Lund, some engineers have recently invented cars and bikes of width 0 (these can travel on lanes with width 0).

The engineers have measured the widths of the cars and bikes in the city. For each pair of important locations, they know the widest car and the widest bike that should be able to travel between them, but the governor also requires that no wider cars or bikes can travel between these two locations.

Formally, you are given for each pair *i*, *j* (0 ≤ *i* < *j* ≤ *N* − 1) two integer values *C<sub>i,j</sub>* and *B<sub>i,j</sub>*. Your task is to construct a network of streets connecting the *N* locations. The streets all have a width of *W*, but for each street *s* you can decide the width of its bike lane *b<sub>s</sub>* and this determines the width of its car lane *W* − *b<sub>s</sub>*. The network must satisfy the following:

*   It must be possible to travel between each pair of locations. Note that this might require a bike or car of width 0.
*   For each pair of locations *i*, *j* (where *i* < *j*), it is possible to travel between *i* and *j* by only using streets whose car lanes have a width of at least *C<sub>i,j</sub>*. Also, *C<sub>i,j</sub>* is the maximum number with this property. That is, for all paths between locations *i* and *j* it holds that at least one of the streets has a car lane of width at most *C<sub>i,j</sub>*.
*   For each pair of locations *i*, *j* (where *i* < *j*), it is possible to travel between *i* and *j* by only using streets whose bike lanes have a width of at least *B<sub>i,j</sub>*. Also, *B<sub>i,j</sub>* is the maximum number with this property.

Can you help the governor of Lund to design such a street network? Because the funding is limited, you can build at most 2023 streets. You can build multiple streets between the same pair of important locations but you cannot connect a location with itself. All streets can be used in both directions.

### Input

The first line of input contains two integers *N* and *W*, the number of important locations in Lund and the width of the streets that you can build.

The following *N* − 1 lines contain the integers *C<sub>i,j</sub>*. The *j*-th of these lines will contain every *C<sub>i,j</sub>* where *i* < *j*. So the first line will contain only *C<sub>0,1</sub>*, the second will contain *C<sub>0,2</sub>* and *C<sub>1,2</sub>*, the third *C<sub>0,3</sub>*, *C<sub>1,3</sub>*, *C<sub>2,3</sub>*, and so on.

The following *N* − 1 lines contain the integers *B<sub>i,j</sub>*, on the same format as *C<sub>i,j</sub>*.

### Output

If it is impossible to construct such a street network, print one line with the string "NO".

Otherwise, print one line with the integer *M*, the number of streets of your network.

For each of the following *M* lines, print three integers *u*, *v*, *b*, indicating that a street with a bike lane of width *b* (and a car lane with width *W* − *b*) goes between *u* and *v*.

You may use at most 2023 streets. The streets you output must satisfy 0 ≤ *b* ≤ *W*, 0 ≤ *u*, *v* ≤ *N* − 1 and *u* ≠ *v*. You may use multiple streets (possibly of different bike lane width) between the same pair of important locations.

In case there are multiple solutions, you may output any of them.

### Constraints and Scoring

*   2 ≤ *N* ≤ 500.
*   1 ≤ *W* ≤ 10<sup>6</sup>.
*   0 ≤ *C<sub>i,j</sub>*, *B<sub>i,j</sub>* ≤ *W* for all 0 ≤ *i* < *j* ≤ *N* − 1.

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

| Group | Score | Limits                                                                   |
|-------|-------|--------------------------------------------------------------------------|
| 1     | 10    | All *C<sub>i,j</sub>* are the same, and all *B<sub>i,j</sub>* are the same, *N* ≤ 40. |
| 2     | 5     | All *C<sub>i,j</sub>* are the same, and all *B<sub>i,j</sub>* are the same.              |
| 3     | 17    | *N* ≤ 40.                                                             |
| 4     | 18    | *W* = 1.                                                                |
| 5     | 19    | All *B<sub>i,j</sub>* are the same.                                              |
| 6     | 31    | No further constraints.                                                  |

### Example

In the first sample, the width of a street is 1 and we need a car lane and a bike lane of width at least 1 between locations 0 and 1. The solution is to have two separate streets connecting the locations, one with a bike lane of width 1 and one with a car lane or width 1.

![](_page_2_Figure_3.jpeg)

In the second sample, the width of a street is again 1 and there should be a path with a bike lane of width 1 between every pair of important locations and there is a path between the locations 1 and 2 and 2 and 3 where the width of the car lane is 1 for every street. This contradicts the fact that, as *C<sub>1,3</sub>* = 0, there should not be a path with car lane width 1 from 1 to 3 as we can just join the two aforementioned paths to form such a path. Thus it is not possible to construct such a street network.

In the third sample, the street network below fulfills all the conditions. For example, there should be a path with minimum width of the car lane 1 = *C<sub>0,5</sub>* between location 0 and location 5 (e.g. by following the route 0 → 2 → 4 → 5), a path where the bike lane has minimum width 3 = *B<sub>0,5</sub>* (e.g. by following the route 0 → 3 → 4 → 5). At the same time it can be checked that there are no paths with a wider minimum width for any of the connections. Note that there are many other solutions to the third sample.

![](_page_3_Figure_0.jpeg)

| Input                                                                            | Output                                                                    |
|----------------------------------------------------------------------------------|---------------------------------------------------------------------------|
| 2 1<br>1<br>1                                                                    | 2<br>0 1 0<br>0 1 1                                                       |
| 4 1<br>0<br>0 1<br>0 0 1<br>1<br>1 1<br>1 1 1                                    | NO                                                                        |
| 6 6<br>5<br>4 4<br>1 1 1<br>1 1 1 3<br>1 1 1 5 3<br>2<br>3 2<br>6 2 3<br>3 2 5 3 | 8<br>0 1 1<br>0 2 3<br>1 2 2<br>0 3 6<br>2 4 5<br>3 4 3<br>3 5 1<br>4 5 4 |

Starter code: