// Unidirectional TSP (one-way travel salesman problem) // PC/Ultraviolet IDs: 111104/116, popularity: A, success rate: Low Level: 3 // verdict: accepted // submission date: 2011-10-12 // UV Run Time: 0.252 s /// copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [question PDF file] // http://uva.onlinejudge.org/external/1/116.pdf//// [solution] // each grid can only accept the route from its left, top left, bottom left, so long as you select the least and the least routes, the right of the current // route passing through this grid is the smallest, and the state transfer equation (using the grid itself to record the right ): /// matrix [I] [J] + = min {matrix [I] [J-1], matrix [I-1] [J-1], matrix [I + 1] [J-1]} // simple DP. However, the condition of the question adds a small unexpected situation. The first line can be connected to the last line, however, this does not add any difficulty. You only need to process the first line and the last line separately. Note: The question requires the least output weight and the path with the smallest Lexicographic Order. If DP is performed from left // to right, although each column and row number is the least, however, the path with the smallest Lexicographic Order cannot be output. The inverse example is as follows: /// 3 4 1 2 8 6 // 1 2 8 2 1 1 // 5 9 3 9 9 5 // 1 1 1 3 1 1 // 3 7 2 8 6 4 //// select the smallest path from left to right, the fourth line is the smallest and the smallest, but the smallest Lexicographic Order is 2-2-1-// 1-2-2. Therefore, you need to perform DP from right to left, this ensures that each time is the smallest and the smallest, and selects the smallest row number to ensure the dictionary // order. Because the number given by the question is not necessarily a positive integer, although the path right and no more than 30 bits, but the number of columns can have a maximum of 100 columns, You Need To // consider using long integers. # Include <iostream> using namespace STD; # define maxline 11 # define maxrow 101 # define maxint (1 <30-1) int main (int ac, char * AV []) {int line, row, Tag, TMP; int successor [maxline] [maxrow]; long matrix [maxline] [maxrow]; long min; while (CIN> line> row) {for (INT I = 1; I <= line; I ++) for (Int J = 1; j <= row; j ++) CIN> matrix [I] [J]; // The column is preferentially processed, starting from the last and second columns, because the minimum permission and path of the Lexicographic Order are required, if processing from left // to right, you can obtain the smallest row number. Permission and scheme, but not necessarily the smallest Lexicographic Order. For (Int J = row-1; j> = 1; j --) {for (INT I = 1; I <= line; I ++) {min = maxint; TMP = (I = 1? Line: I-1); If (min> matrix [TMP] [J + 1]) {min = matrix [TMP] [J + 1]; tag = TMP ;} else if (min = matrix [TMP] [J + 1] & tag> TMP) Tag = TMP; If (min> matrix [I] [J + 1]) {min = matrix [I] [J + 1]; tag = I;} else if (min = matrix [I] [J + 1] & tag> I) tag = I; TMP = (I = line? 1: I + 1); If (min> matrix [TMP] [J + 1]) {min = matrix [TMP] [J + 1]; tag = TMP ;} else if (min = matrix [TMP] [J + 1] & tag> TMP) Tag = TMP; matrix [I] [J] + = min; successor [I] [J] = tag ;}// find the right and the smallest cell. Min = maxint; For (INT I = 1; I <= line; I ++) if (Matrix [I] [1] <min) {min = matrix [I] [1]; tag = I ;}// output. Cout <tag; int next = 1; int TMP = tag; while (next <row) {cout <"" <successor [TMP] [next]; TMP = successor [TMP] [next]; next ++;} cout <Endl; cout <matrix [tag] [1] <Endl;} return 0 ;}