Ultraviolet A Problem 116 unidirectional TSP (one-way Traveling Salesman Problem)

Source: Internet
Author: User
Tags dot net
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.