2017 popular group Los D1T3 Valley P3956 board
Title Description
There is a m × checkerboard m, each lattice on the board may be red, yellow or no color. You are now the lower right corner from the top left corner of the board most come to the board.
Any moment, you are standing position must be colored (not colorless), you can only up, down, left, and right direction. When you go to the other grid from a grid, if two cells of the same color, then you do not need to spend gold; If not, you need to spend 1 gold coin.
In addition, you can spend 2 gold coins under his magic to make a colorless grid temporarily become your specified color. But the magic can not be used continuously, and the duration of this magic is very short, that is, if you use this magic and went out on the grid temporarily colored, you will not be able to continue to use magic; only when you leave this position , already has come a time on a grid of colors, you can continue to use this magic, and when you leave this position (making changes to his magic colored plaid) when the grid returns to colorless.
Now you want the upper left corner of the board, most went to the bottom right corner of the board, seeking a minimum cost of gold is how much?
Input and output formats
Input formats:
The first line contains two positive integers m, n-, separated with a space, representing the size of the board, there is the number of colors on the board grid.
The next n- n-rows, each row of three positive integers X, Y, C , respectively, coordinates (x, y) grid color C .
Where c = 1 for yellow, C = 0 for red. Separated by a space between two adjacent numbers. The coordinates of the upper left corner of the board (1, 1) , the coordinates of the lower right corner (m, m) .
The rest of the grid on the board are colorless. Ensure the top left corner of the board, that is, (1, 1) there must be some color.
Output formats:
An integer representing the minimum cost of gold, if not reach, output -1 - 1
Sample input and output
Explanation
Sample Description 1 O
From ( 1 , 1 ) start, walked ( 1 , 2 ) does not cost gold
From ( 1 , 2 went) down ( 2 , 2 ) cost 1 gold coins
From ( 2 , 2 ) to display their magic, the ( 2 , 3 ) turns yellow, take 2 gold coins
From ( 2 , 2 ) come ( 2 , 3 ) does not cost gold
From ( 2 , 3 ) come ( 3 , 3 ) does not cost gold
From the ( 3 , 3 ) go ( 3 , 4 ) takes a gold coin
From ( 3 , 4 ) come ( 4 , 4 ) takes one gold coins
From ( 4 , 4 ) cast magic, the ( 4 , 5 ) to yellow, to spend two coins,
From ( 4 , 4 ) come ( 4 , 5 ) does not cost gold
From ( 4 , 5 ) went to ( 5 , 5 ) takes 1 gold coins
It spent a total of 8 gold coins.
Sample 2 illustrates the input-output
From ( 1 , 1 ) go ( 1 , 2 ), does not cost gold
From ( 1 , 2 ) come ( 2 , 2 ), takes 1 gold
The cast magic ( 2 , 3 ) to yellow,
And from ( 2 , 2 ) come ( 2 , 3 ) cost 2 gold
From ( 2 , 3 ) come ( 3 , 3 ) does not cost gold
From ( 3 , 3 ) can reach the cast magic ( 3 , 2 ) , ( 2 , 3 ) , ( 3 , 4 ) , ( 4 , 3 )
From the above four points are not reach ( 5 , 5 ), it can not reach the end, the output - . 1
Data size and convention
For . 3 0 % of the data, . 1 ≤ m ≤. 5, n-≤ ≤ 10. 1 . 1 ≤ m ≤ . 5 , . 1 ≤ n- ≤ . 1 0.
For . 6 0 % of the data, . 1 ≤ m ≤ 20 is,. 1 ≤ ≤ n-200 is . 1 ≤ m ≤ 2 0 , . 1 ≤ n- ≤ 2 0 0.
For . 1 0 0 % of the data, . 1 ≤ m ≤ 100,. 1 ≤ ≤ n-1,000 . 1 ≤ m ≤ . 1 0 0 , . 1 ≤ n- ≤ . 1 , 0 0 0.
Here is my method slightly!
According to legend, this is what the ghost of Dijkstra's algorithm
Dijkstra (Dijkstra)
https://www.cnblogs.com/jason2003/p/7222182.html
Okay, it seems to have started playing a graph theory