#P7936. [COCI 2007/2008 #5] BARICA

[COCI 2007/2008 #5] BARICA

Problem Description

Barica is an unusual frog. She lives in a pond with NN lily pads floating on the water surface. The lily pads are numbered from 11 to NN, and their positions can be described by a pair of coordinates. Barica can jump between these lily pads, but she is afraid of jumping diagonally and of jumping in the negative direction.

More precisely, she can jump from coordinate (x1,y1)(x_1,y_1) to another coordinate (x2,y2)(x_2,y_2) only if:

  • x2>x1x_2>x_1 and y2=y1y_2=y_1, or
  • y2>y1y_2>y_1 and x2=x1x_2=x_1.

For each lily pad, we know the number of flies nearby. Barica can eat all the flies near the lily pad she is on.

For each fly Barica eats, she gains 11 unit of energy. Each jump costs KK units of energy. If Barica does not have enough energy, she cannot jump.

Barica wants to travel from lily pad 11 to lily pad NN and end up with as much energy as possible. Barica starts with no energy and must gain energy by eating the flies near lily pad 11.

Find a feasible path that allows Barica to reach the goal.

Input Format

The first line contains two integers NN and KK.

The next NN lines each contain three integers xix_i, yiy_i, and ziz_i, meaning that lily pad ii is at (xi,yi)(x_i,y_i) and there are ziz_i flies nearby.

Output Format

The first line outputs the amount of energy units after reaching the destination.

The second line outputs an integer LL, the number of lily pads Barica needs to pass through. It must include lily pads 11 and NN.

The next LL lines output, in order, the coordinates of the lily pads Barica should pass through.

6 5
1 1 5
2 1 5
1 2 4
2 3 5
3 2 30
3 3 5 
5
4
1 1
2 1
2 3
3 3 
8 10
1 1 15
2 2 30
1 2 8
2 1 7
3 2 8
2 3 7
4 2 100
3 3 15
36
5
1 1
1 2
2 2
3 2
3 3
9 5
5 5 10
6 5 2
7 5 1
5 6 2
6 6 6
7 6 2
5 7 1
6 7 2
7 7 1 
2
3
5 5
7 5
7 7 

Hint

Constraints: For 100%100\% of the data, 2N3×1052\le N\le 3\times 10^5, 1K10001\le K\le 1000, 0xi,yi1050\le x_i,y_i\le 10^5, 0zi10000\le z_i\le 1000.

The input guarantees that a solution exists, but it is not necessarily unique.

The score for this problem follows the original contest setting, with a full score of 7070 points.

Translated by ChatGPT 5