问题描述:

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 below.

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 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.

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

题意:

给你个二维数组,你可以从第一列第 i 行出发向下一行行走,但每次只能到达下一行的第 i 行或与它相邻的第 i-1或 i+1行(这里第 1 行与第 n 行看做是相邻的)。问怎么走才能使路径上数字和最小,先把路径输出出来,再把最小的和输出出来。

思路 / 方法:

假设你在第 j 列第 i 行,在第 j-1 列则有第 i-1,i,i+1行可以走到当前位置,为了使路径数字和最小,应该选择到达这三个位置是路径数字总和最短的一个走到当前位置,如果数字总和相同保留字典序较小的路径,另外两个就可以舍弃掉。dp[ i ][ j ] = min( dp[ i-1 ][ j-1 ] , dp[ i ][ j-1 ] , dp[ i+1 ][ j-1 ]) + k [ i ][ j ]。

代码:

#define N 12
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[N][10*N];//记录到达当前位置使,所走路径上的数字总和
int k[N][10*N];
int f[N][10*N];//记录路径
int l[10*N];
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)){for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)scanf("%d",&k[i][j]);for(int i=1; i<=n; i++){dp[i][1]=k[i][1];//第一列路径上的数字f[i][1]=i;       //第一列的路径就是当前位置}int mi,flag;for(int j=2; j<=m; j++){mi=0x3f3f3f3f;//下面就是 dp[i][j]=min(dp[i-1][j-1],dp[i][j-1],dp[i+1][j-1])+k[i][j]for(int i=1; i<=n; i++){int d;if(i==1){d=i;if(n>1&&dp[i+1][j-1]<dp[d][j-1])d=i+1;if(dp[n][j-1]<dp[d][j-1])d=n;}else if(i==n){d=1;if(n>1&&dp[n-1][j-1]<dp[d][j-1])d=n-1;if(dp[n][j-1]<dp[d][j-1])d=n;}else{d=i-1;if(dp[i][j-1]<dp[d][j-1])d=i;if(dp[i+1][j-1]<dp[d][j-1])d=i+1;}dp[i][j]=dp[d][j-1]+k[i][j];//保留最小的路径数字总和f[i][j]=d;                  //记录是从哪一行走到当前位置if(j==m&&dp[i][j]<mi)       //走到尽头时,记录下路径上数字最小的在哪一行{mi=dp[i][j];flag=i;}}}int d=dp[flag][m];for(int i=m; i>=1; i--){l[i]=flag;//把路径存到 l 数组中,便于输出flag=f[flag][i];//寻找上一步的路径}for(int i=1; i<=m; i++)//输出路径{if(i!=m)printf("%d ",l[i]);elseprintf("%d",l[i]);}printf("\n%d\n",d);}return 0;
}

Unidirectional TSP—dp相关推荐

  1. UVA 116 Unidirectional TSP DP

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&p ...

  2. Unidirectional TSP UVA - 116(多阶段决策+输出字典序最小的路径)

    Unidirectional TSP UVA - 116 题意: 给你m行n列的矩阵,从第一列出发,到最后一列. 要求 经过的整数和最小. 输出路径上,每列的行号,多解时,输出字典序最小的. 思路: ...

  3. UVA 116 Unidirectional TSP (白书dp)

    http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=18206 1 /* 2 题目大意: 3 从第一列的任意一格出发,到子最 ...

  4. UVA116 单向TSP Unidirectional TSP(多阶段决策问题、输出字典序最小的方案、DAG上DP)

    整理的算法模板合集: ACM模板 题目传送门 简单的多阶段决策问题. 多段图DAG,其节点可以划分为若干个阶段,每一个阶段只由上一个阶段所决定. 因为本题要求输出从前到后的方案,所以我们转移方程的时候 ...

  5. UVA 116——Unidirectional TSP

    题意:给定一个n*m的矩阵,从第一列任意位置向右,右下,右上走一格,可以循环,要求经过的数字的和最小. 思路:记忆化搜索.每次有三个状态,从这三个状态中选择出最优的一个,然后状态累加,保存路径. co ...

  6. UVa 116 (多段图的最短路) Unidirectional TSP

    题意: 有一个m行n列的正整数环形矩阵(即矩阵第一行的上一行是最后一行,最后一行的下一行是第一行),从第一列的任意位置出发,每次只能向右,右上,右下三个方向行走,输出路径及路径上所有数之和的最大值,多 ...

  7. 个人DP训练(基础版)

    题目链接    hdu 2955 Robberies 01背包,转化为求被抓的概率. 题目链接       hdu   1864  最大报销额 01背包,每张发票的总额为容量和价值,注意预处理数据,对 ...

  8. DP(动态规划)总结

    1. 写在前面的话 之前写了一篇不像总结的动态规划总结,感觉更像是一个成长历程,所以就打算重写一篇. 2. 对DP简单的总结 dp的题目特点 求最大或者最小值(如背包:价值最大.凑硬币:数量最少--) ...

  9. 提高篇 第五部分 动态规划 第4章 状态压缩类动态规划

    例1 骑士(Sgu223) 1592:[例 1]国王 信息学奥赛一本通(C++版)在线评测系统 https://blog.csdn.net/guoyangfan_/article/details/82 ...

最新文章

  1. 康托展开式---我排第几+逆康托展开
  2. 神秘大三角(判断点与三角形的关系)
  3. 使用putty连接本地VirtualBox上的centos7 linux主机
  4. Axis2 生成客户端
  5. java图片像素90翻转_java后台解决上传图片翻转90的问题,有demo,经过测试可用...
  6. kuangbin 基础DP1
  7. 手机投屏到电脑的5种方式,你学到了吗!
  8. JIRA导出工作日志到Excel
  9. html批量修改 快捷键,html怎么设置快捷键?
  10. 在线靶场-墨者-网络安全2星-某防火墙默认口令
  11. 计算机笔记--【Redis高级】
  12. C语言,百马百担暴力秒懂,有100匹马,驮100担货,大马驮3担,中马驮2担,两匹小马驮1担,要求一次性驮完,请问大马、中马、小马各几匹?
  13. 读书笔记(二十二):前端安全
  14. coreldraw2023安装教程及新功能讲解
  15. 订餐系统jsp模板_基于JSP的网上订餐系统的设计与实现
  16. vmware设置共享文件夹
  17. 一加5t刷android p,一加5T刷机包
  18. 训练神经网络的详细步骤,人工神经网络训练过程
  19. Java JDK1.8帮助文档API下载
  20. TL摄像头如何放到html去直播,教程丨怎么把摄像头的画面,传到抖音直播里?

热门文章

  1. 复合头肩型态(转载)
  2. 校正Ubuntu时间为北京时间
  3. 激光雷达数据处理常用软件
  4. vue实现tree-table
  5. 讯飞语音输入简单使用
  6. win10计算机怎么显示桌面,win10如何显示我的电脑在桌面?教您显示的方法
  7. win10家庭版无法关闭windows defender的解决办法,网上流传的方法试了均不行
  8. 基于android驾校模拟考试系统app
  9. 认真学习前端第二周学习笔记(浮动,定位,精灵图,布局)
  10. iPic 添加 阿里云OSS图床