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)

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

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

最小的路径

细节见代码

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const int MAXN = + ;
const int INF = 0x3f3f3f3f;
const int dir[] = {-,,}; int dp[MAXN][MAXN], nxt[MAXN][MAXN], g[MAXN][MAXN], n, m; int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
for(register int i = ;i <= n;++ i)
for(register int j = ;j <= m;++ j)
read(g[i][j]);
for(register int i = ;i <= n;++ i)
dp[i][m] = g[i][m];
for(register int i = m - ;i >= ;-- i)
for(register int j = ;j <= n;++j)
{
dp[j][i] = INF;
int row[] = {(j - + n) % n + , (j + n) % n + , (j - + n) % n + };
std::sort(row, row + );
for(register int k = ;k < ;++ k)
{
int tmp = row[k];
if(dp[j][i] > dp[tmp][i + ] + g[j][i])dp[j][i] = dp[tmp][i + ] + g[j][i], nxt[j][i] = tmp;
}
}
int ans = INF, pos = ;
for(register int i = ;i <= n;++ i)
if(ans > dp[i][])ans = dp[i][], pos = i;
printf("%d", pos);
for(register int i = ;i < m;++ i)
printf(" %d", pos = nxt[pos][i]);
putchar('\n');
printf("%d\n", ans);
}
return ;
}

Uva116

最新文章

  1. runtime梳理。
  2. mysqli链接数据库:面向对象
  3. BZOJ3838 : [Pa2013]Raper
  4. O2O模式成功案例分享 汲取精华化为己用
  5. android backlight
  6. QWebView 显示本地HTML 文件
  7. Collections之sort的两个方法(自然排序和自定义比较器排序)
  8. Linux 环境下 fork 函数和 exec 函数族的使用
  9. Git常用命令(转)
  10. 如何对Javascript代码进行二次压缩(混淆)
  11. 判断pc浏览器和手机浏览器方法
  12. 5个为什么(five-whys)
  13. Java之戳中痛点 - (6)避免类型自动转换,例如两个整数相除得浮点数遇坑
  14. python当中的 可迭代对象 迭代器
  15. C#中关于表达式与委托在EF中的不同表现总结
  16. 解决ubuntu16.04桌面左侧栏和顶部栏消失的问题
  17. w7 全网架构-rsync-备份
  18. SAP CRM 开发学习资料和教程整理【不定时更新】
  19. python中的 sql语句用法
  20. Linux基础命令---检查密码文件pwck

热门文章

  1. VUE环境下获取当前时间并格式化--按秒数更新
  2. &lt;&lt;十二怒汉&gt;&gt;影评——程序正义,结果正义?
  3. Java-MyBatis-MyBatis3-XML映射文件:参数
  4. PAT甲级——A1088 Rational Arithmetic
  5. &lt;每日一题&gt;题目7:简单的学生管理系统V1.0
  6. [转]Visual Studio 2010生成解决方案时,提示磁盘空间不足!
  7. bzoj1706 relays 奶牛接力跑 线性代数
  8. Ionic跳转到外网地址
  9. MUI使用vue示例
  10. 使用alibaba的json工具将String类型转为JSONArray类型