博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Uva116 Unidirectional TSP
阅读量:5148 次
发布时间:2019-06-13

本文共 5366 字,大约阅读时间需要 17 分钟。

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 
2 #include
3 #include
4 #include
5 #include
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

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7684679.html

你可能感兴趣的文章
inline函数的总结
查看>>
Python字符编码
查看>>
leetcode 49. 字母异位词分组(Group Anagrams)
查看>>
NSPredicate的使用,超级强大
查看>>
自动分割mp3等音频视频文件的脚本
查看>>
财务结算的目的和一般流程
查看>>
判断字符串是否为空的注意事项
查看>>
布兰诗歌
查看>>
老李分享:5个衡量软件质量的标准
查看>>
Xcode部分插件无法使用识别的问题
查看>>
set学习记录
查看>>
用函数写注册功能
查看>>
JVM笔记4:Java内存分配策略
查看>>
IE8 window.open 不支持此接口 的问题解决
查看>>
Django -- 发送HTML格式的邮件
查看>>
最近面试问题汇总
查看>>
ORM版学员管理系统3
查看>>
修改安卓虚拟机系统镜像
查看>>
windows 2003 Server平台Delphi程序不支持直接调用webservice
查看>>
电子书下载:Professional ASP.NET Design Patterns
查看>>