UVa unidirectional TSP (DP)

Source: Internet
Author: User

The problem is an example of the beginner's Classic (second edition) of Algorithmic competition, which is not very difficult. I first did not see the solution to do it myself, although eventually passed, the idea and the book is the same. But more complicated than the code in the book, it can be seen that their handling of the problem is still deficient.

The problem is similar to a digital triangle, and the way to do this is to step from the penultimate column to the first column, each time selecting the path with the lowest weight in the following column. Finally find the one that costs the least. Because there are multiple answers to the output of the dictionary before the path of the test, so when the path is selected when the same, to select the small number of rows. I used a lot of conditional statements to deal with this problem, making the final code very long. Here's My Code:

  

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 Long Longa[ A][ the];8 intpre[ A][ the];9 Ten intMain () One { A     intm, N, I, J, TEMW, Temr; -  -      while(SCANF ("%d%d", &m, &n) = =2) the     { -          for(i =1; I <= m; i++) -         { -              for(j =1; J <= N; J + +) +             { -scanf"%lld", &a[i][j]); +             } A         } atmemset (PRE,0,sizeof(pre)); -          for(j = N1; J >=1; j--) -         { -              for(i =1; I <= m; i++) -             { -                 //first consider the path to the upward direction in                 if(i = =1)//first line, go up to the last line -                 { toTEMW = A[m][j +1]; +TEMR =m; -                 } the                 Else *                 { $TEMW = A[i-1][j +1];Panax NotoginsengTEMR = i-1; -                 } the                 //consider the path to the forward direction +                 if(A[i][j +1] <TEMW) A                 { theTEMW = A[i][j +1]; +TEMR =i; -                 } $                 Else if(A[i][j +1] ==TEMW) $                 { -Temr = min (Temr, i);//equal row numbers are small.  -                 } the                 //consider the path in the downward direction -                 if(i = = m)//last line, go down to the first lineWuyi                 { the                     if(a[1][j +1] <TEMW) -                     { WuTEMW = a[1][j +1]; -TEMR =1; About                     } $                     Else if(a[1][j +1] ==TEMW) -                     { -Temr = min (Temr,1); -                     } A                 } +                 Else the                 { -                     if(A[i +1][j +1] <TEMW) $                     { theTEMW = A[i +1][j +1]; theTEMR = i +1; the                     } the                     Else if(A[i +1][j +1] ==TEMW) -                     { inTemr = min (temr, i +1); the                     } the                 } AboutA[I][J] + =TEMW; thePRE[I][J] = TEMR;//Record Path the             } the         } +TEMW = a[1][1]; -TEMR =1; the          for(i =2; I <= m; i++)//looking for the smallest. Bayi         { the             if(a[i][1] <TEMW) the             { -TEMW = a[i][1]; -TEMR =i; the             } the         } the         //Output Path the         if(n = =1) -         { theprintf"%d\n", TEMR); the         } the         Else94         { the              for(i = temr, j =1; J <= N-1;) the             { theprintf"%d", i);98i =Pre[i][j]; AboutJ + +; -             }101printf"%d\n", i);102         }103printf"%lld\n", a[temr][1]);104     } the     return 0;106}

Obviously, in dealing with three directions, a lot of code is used, and that's what the book does:

  

1 introws[3] = {I, I-1, i+1};//line number in normal case2 if(i = =0)//The first line of the book is 0, and if it is the first row, the upward line number needs to be changed. 3rows[1] = M-1;//last Column column number is m-14 if(i = = m1)5rows[2] =0;6D[I][J] = INF;//Array d is used to store weights7Sort (rows, rows+3);//sort First, then compare8  for(intK =0; K <3; k++)9 {Ten     intv = d[rows[k]][j+1] +A[i][j]; One     if(V <D[i][j]) A     { -D[I][J] =v; -NEXT[I][J] =Rows[k]; the     } -}

Obviously, the book by the first sort and then compare, so from the smallest line number to find the minimum weight, find the minimum weight must be the smallest line number, reduce a lot of code.

Also need to say is, this problem I first read wrong TEST instructions!!! This is the big taboo of the game! Depending on the diagram given, it is assumed that the first column of the first row starts with the last row. By the way, I'm thinking about this situation. In this case, a bool array is required to mark whether each point is reachable, first mark the end to reach, then loop from the penultimate column, select a path that can be reached in the next column, with the lowest weight, the smallest line number, and mark the point as reachable, and mark the point as unreachable if there are no reachable points at the back. When you loop to the first column, you only need to consider the starting point. Other similar to the original question.

UVa unidirectional TSP (DP)

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.