码迷,mamicode.com
首页 > 其他好文 > 详细

Uva116 Unidirectional TSP

时间:2017-10-18 02:11:53      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:src   ons   splay   numbers   ros   where   class   dea   seq   

https://odzkskevi.qnssl.com/292ca2c84ab5bd27a2a91d66827dd320?v=1508162936

https://vjudge.net/problem/UVA-116

Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Salesperson Problem (TSP) — finding whether all the cities in a salesperson’s route can be visited exactly once with a specified limit on travel time — is one of the canonical examples of an NP-complete problem; solutions appear to require an inordinate amount of time to generate, but are simple to check. This problem deals with finding a minimal path through a grid of points while traveling only from left to right. Given an m×n matrix of integers, you are to write a program that computes a path of minimal weight. A path starts anywhere in column 1 (the first column) and consists of a sequence of steps terminating in column n (the last column). A step consists of traveling from column i to column i + 1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 and m) of a matrix are considered adjacent, i.e., the matrix “wraps” so that it represents a horizontal cylinder. Legal steps are illustrated on the right. The weight of a path is the sum of the integers in each of the n cells of the matrix that are visited. For example, two slightly different 5×6 matrices are shown below (the only difference is the numbers in the bottom row). The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows. Input The input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by m · n integers where m is the row dimension and n is the column dimension. The integers appear in the input in row major order, i.e., the first n integers constitute the first row of the matrix, the second n integers constitute the second row and so on. The integers on a line will be separated from other integers by one or more spaces. Note: integers are not restricted to being positive. There will be one or more matrix specifications in an input file. Input is terminated by end-of-file. For each specification the number of rows will be between 1 and 10 inclusive; the number of columns will be between 1 and 100 inclusive. No path’s weight will exceed integer values representable using 30 bits. Output Two lines should be output for each matrix specification in the input file, the first line represents a minimal-weight path, and the second line is the cost of a minimal path. The path consists of a sequence of n integers (separated by one or more spaces) representing the rows that constitute the minimal path. If there is more than one path of minimal weight the path that is lexicographically smallest should be output. Note: Lexicographically means the natural order on sequences induced by the order on their elements. Sample Input 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 8 6 4 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 1 2 3 2 2 9 10 9 10 Sample Output 1 2 3 4 4 5 16 1 2 1 5 4 5 11 1 1 19

 

【题解】

dp[i][j]表示从开始到(i,j)的最短长度,nxt[i][j]表示从(nxt[i][j],j - 1)走到了(i, j)

题目要求输出字典序最小的路径

那我们只有倒推后继,保证每个后继的字典序最小,才能在第一列选出字典序

最小的路径

细节见代码

 

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #define max(a, b) ((a) > (b) ? (a) : (b))
 7 #define min(a, b) ((a) < (b) ? (a) : (b))
 8 inline void swap(int &a, int &b)
 9 {
10     int tmp = a;a = b;b = tmp;
11 }
12 inline void read(int &x)
13 {
14     x = 0;char ch = getchar(), c = ch;
15     while(ch < 0 || ch > 9)c = ch, ch = getchar();
16     while(ch <= 9 && ch >= 0)x = x * 10 + ch - 0, ch = getchar();
17     if(c == -)x = -x;
18 }
19 
20 const int MAXN = 100 + 10;
21 const int INF = 0x3f3f3f3f;
22 const int dir[3] = {-1,0,1};
23 
24 int dp[MAXN][MAXN], nxt[MAXN][MAXN], g[MAXN][MAXN], n, m;
25 
26 int main()
27 {
28     while(scanf("%d %d", &n, &m) != EOF)
29     {
30         for(register int i = 1;i <= n;++ i)
31             for(register int j = 1;j <= m;++ j)
32                 read(g[i][j]);
33         for(register int i = 1;i <= n;++ i)
34             dp[i][m] = g[i][m];
35         for(register int i = m - 1;i >= 1;-- i)
36             for(register int j = 1;j <= n;++j)
37             {
38                 dp[j][i] = INF;
39                 int row[3] = {(j - 1 + n) % n + 1, (j + n) % n + 1, (j - 2 + n) % n + 1};
40                 std::sort(row, row + 3);
41                 for(register int k = 0;k < 3;++ k)
42                 {
43                     int tmp = row[k];
44                     if(dp[j][i] > dp[tmp][i + 1] + g[j][i])dp[j][i] = dp[tmp][i + 1] + g[j][i], nxt[j][i] = tmp;
45                 }
46             }
47         int ans = INF, pos = 0;
48         for(register int i = 1;i <= n;++ i)
49             if(ans > dp[i][1])ans = dp[i][1], pos = i;
50         printf("%d", pos);
51         for(register int i = 1;i < m;++ i)
52             printf(" %d", pos = nxt[pos][i]);
53         putchar(\n);
54         printf("%d\n", ans);
55     }
56     return 0;
57 }
Uva116

 

Uva116 Unidirectional TSP

标签:src   ons   splay   numbers   ros   where   class   dea   seq   

原文地址:http://www.cnblogs.com/huibixiaoxing/p/7684679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!