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

本文详细解析了UVA116-UnidirectionalTSP问题的解决方法,通过动态规划求解最小路径权重及其路径,代码实现清晰,适合算法初学者学习。
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 }

 

转载于:https://www.cnblogs.com/npugen/p/9727322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值