luogu#P9466. [EGOI 2023] Bikes vs Cars / 骑车与汽车

    ID: 8935 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2023Special JudgeO2优化EGOI(欧洲/女生)

[EGOI 2023] Bikes vs Cars / 骑车与汽车

Background

Day 1 Problem D.

Translated from EGOI2023 bikesvscars.

CC BY-SA 3.0

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 NN key locations in Lund (numbered from 00 to N1N-1), 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 WW. 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 00 (they can travel on roads of width 00).

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 i,ji,j (0i<jN10\le i < j\le N-1), you are given two integers Ci,jC_{i,j} and Bi,jB_{i,j}. Your task is to construct a street network connecting these NN locations. All streets have total width WW, but for any street ss, you may choose its bicycle-lane width bsb_s and car-lane width WbsW-b_s. The network must satisfy:

  • It must be possible to travel between any two locations. This may require a bicycle or car of width 00.
  • For each pair of locations i,ji,j (where i<ji<j), it must be possible to travel between ii and jj using only car lanes of width at least Ci,jC_{i,j}. Also, Ci,jC_{i,j} is the largest number with this property. This means that for every path between ii and jj, there must be at least one street whose car-lane width is at most Ci,jC_{i,j}.
  • For each pair of locations i,ji,j (where i<ji<j), it must be possible to travel between ii and jj using only bicycle lanes of width at least Bi,jB_{i,j}. Also, Bi,jB_{i,j} 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 20232023 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 N,WN,W, the number of key locations and the street width.

The next N1N-1 lines contain Ci,jC_{i,j}. The jj-th of these lines contains all Ci,jC_{i,j} with i<ji<j. Therefore, the first line contains only C0,1C_{0,1}, the second line contains C0,2C_{0,2} and C1,2C_{1,2}, the third line contains C0,3,C1,3,C2,3C_{0,3},C_{1,3},C_{2,3}, and so on.

The next N1N-1 lines contain Bi,jB_{i,j}, in the same format as Ci,jC_{i,j}.

Output Format

If no street network satisfying the requirements exists, output NO.

Otherwise, output an integer MM on the first line, the number of streets you build.

Then output MM lines, each containing three integers u,v,bu,v,b, describing a street between uu and vv whose bicycle-lane width is bb (and car-lane width is WbW-b).

You may use at most 20232023 streets. The streets you output must satisfy 0bW0\le b\le W, 0u,vN10\le u,v\le N-1, and uvu\ne v. 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 11, the street width is 11. We need a bicycle lane of width at least 11 and a car lane of width at least 11 between 00 and 11. The solution is to connect them with two separate streets: one with bicycle-lane width 11, and the other with car-lane width 11.


Explanation of Sample 2

In sample 22, the street width is still 11. We need a bicycle lane of width 11 connecting every pair of key locations, and a car lane of width 11 connecting 1,21,2 and 2,32,3. This contradicts C1,3=0C_{1,3}=0: there should not be a car-lane path of width 11 from 11 to 33, 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 33, the street network in the figure satisfies all requirements. For example, there should be a car-lane path of width 1=C0,51=C_{0,5} between 00 and 55 (path 02450\to 2\to 4\to 5), and a bicycle-lane path of width 3=B0,53=B_{0,5} between 00 and 55 (path 03450\to 3\to 4\to 5). Also, one can verify that there is no path with a larger minimum width. Note that sample 33 may have many other valid solutions.


Constraints

For all testdata, 2N5002\le N\le 500, 1W1061\le W\le 10^6, 0Ci,j,Bi,jW0\le C_{i,j},B_{i,j}\le W.

  • Subtask 1 (1010 points): all Ci,jC_{i,j} are equal, all Bi,jB_{i,j} are equal, N40N\le 40.
  • Subtask 2 (55 points): all Ci,jC_{i,j} are equal, all Bi,jB_{i,j} are equal.
  • Subtask 3 (1717 points): N40N\le 40.
  • Subtask 4 (1818 points): W=1W=1.
  • Subtask 5 (1919 points): all Bi,jB_{i,j} are equal.
  • Subtask 6 (3131 points): no special constraints.

Translated by ChatGPT 5