UVA116-Unidirectional TSP(动态规划基础)

Problem UVA116-Unidirectional TSP

Accept: 7167  Submit: 56893
Time Limit: 3000 mSec

Problem Description

 

 

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. Foreachspecificationthenumberofrowswillbebetween1and10inclusive; thenumberofcolumns 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-weightpath, andthesecondlineisthecostofaminimalpath. Thepathconsistsofasequence of n integers(separatedbyoneormorespaces)representingtherowsthatconstitutetheminimalpath. 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

 

题解:和数字三角形一样,水题。

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxn = 100 + 5, maxm = 10 + 5;
 6 const int INF = 0x3f3f3f3f;
 7 
 8 int n, m;
 9 int val[maxm][maxn], dp[maxm][maxn];
10 int Next[maxm][maxn];
11 
12 int read() {
13     int q = 0, f = 1; char ch = ' ';
14     while (ch<'0' || ch>'9') {
15         if (ch == '-') f = -1;
16         ch = getchar();
17     }
18     while ('0' <= ch && ch <= '9') {
19         q = q * 10 + ch - '0';
20         ch = getchar();
21     }
22     return q * f;
23 }
24 
25 int main()
26 {
27     //freopen("input.txt", "r", stdin);
28     while (~scanf("%d%d", &m, &n)) {
29         for (int i = 0; i < m; i++) {
30             for (int j = 0; j < n; j++) {
31                 val[i][j] = read();
32             }
33         }
34         //memset(dp, INF, sizeof(dp));
35         int ans = INF, first = -1;
36 
37         for (int j = n - 1; j >= 0; j--) {
38             for (int i = 0; i < m; i++) {
39                 if (j == n - 1) {
40                     dp[i][j] = val[i][j];
41                 }
42                 else {
43                     int row[3] = { (i - 1 + m) % m,i,(i + 1) % m };
44                     sort(row, row + 3);
45                     dp[i][j] = INF;
46                     for (int k = 0; k < 3; k++) {
47                         if (dp[i][j] > dp[row[k]][j + 1] + val[i][j]) {
48                             dp[i][j] = dp[row[k]][j + 1] + val[i][j];
49                             Next[i][j] = row[k];
50                         }
51                     }
52                 }
53                 if (j == 0 && dp[i][j] < ans) {
54                     ans = dp[i][j];
55                     first = i;
56                 }
57             }
58         }
59 
60         printf("%d", first + 1);
61         for (int i = Next[first][0], j = 1; j < n; i = Next[i][j], j++) {
62             printf(" %d", i + 1);
63         }
64         printf("\n%d\n", ans);
65     }
66     return 0;
67 }

 

posted on 2018-09-30 01:45  随缘&不屈  阅读(466)  评论(1编辑  收藏  举报

导航