Uva116 Unidirectional TSP
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
Uva116 Unidirectional TSP的更多相关文章
- UVa-116 Unidirectional TSP 单向旅行商
题目 https://vjudge.net/problem/uva-116 分析 设d[i][j]为从(i,j)到最后一列的最小开销,则d[i][j]=a[i][j]+max(d[i+1][j+1], ...
- UVA116 Unidirectional TSP 单向TSP
分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...
- HDU 1619 Unidirectional TSP(单向TSP + 路径打印)
Unidirectional TSP Problem Description Problems that require minimum paths through some domain appea ...
- uva 116 Unidirectional TSP (DP)
uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...
- UVA 116 Unidirectional TSP(dp + 数塔问题)
Unidirectional TSP Background Problems that require minimum paths through some domain appear in ma ...
- UVA 116 Unidirectional TSP(DP最短路字典序)
Description Unidirectional TSP Background Problems that require minimum paths through some domai ...
- uva 116 Unidirectional TSP【号码塔+打印路径】
主题: uva 116 Unidirectional TSP 意甲冠军:给定一个矩阵,当前格儿童值三个方向回格最小值和当前的和,就第一列的最小值并打印路径(同样则去字典序最小的). 分析:刚開始想错了 ...
- 9-4 Unidirectional TSP uva116 (DP)
题意:给一个n行m列矩阵 从第一列任意一个位置出发 每次往右 右上 右下三个方向走一格 直到最后一列 输出所类和的最小值和路径!! 最小值相同则输出字典序最小路径 很像一开始介绍的三角形dp ...
- UVA 116 Unidirectional TSP 经典dp题
题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小. 用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...
随机推荐
- 19-11-08-Night
再咕咕咕会被爆捶吗??? ZJ: 喜闻乐见: 27 Miemeng 60 01:59:43 100 01:59:44 0 01:59:44 160 01:59:44 最水的$T1$挂了???? $T2 ...
- 关于 ros
1.https://mikrotik.com/download 下载 x86 架构的 cd image (当日这是试用版,特殊版下载后道理一样) 2.exsi 上传,并新建 linux 的 其他 ...
- Spring框架使用ByName自动注入同名问题剖析
问题描述 我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...
- 更改git提交显示的用户名
问题描述 同一项目多人开发难免会用到版本控制,最为流行的当属git.开发中出现一个小问题,每个人提交后显示的用户名,如下图 组长发话:把用户名都改成自己的名字! 这时发现用户名并不是自己的名字,怎么改 ...
- Error: Could not link: /usr/local/share/doc/homebrew
mac 执行brew update 报错 Error: Could not link: /usr/local/share/doc/homebrew 更新brew,报错 Error: Could not ...
- vue 路由跳转记住滚动位置,返回时回到上次滚动位置
参考:https://blog.csdn.net/qq_40204835/article/details/79853685 方法一: 利用Keep-Alive和监听器 1.首先在路由中引入需要的模块 ...
- 关于python DataFrame的学习记录
df_1 = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]}) print df_1 默认左边行index0往上递增,AB为顶部标识,数组内为内容 loc— ...
- css-文本两行或多行文本溢出显示省略号(转)
转自:http://www.daqianduan.com/6179.html 感谢作者 1.单行文本的溢出显示省略号 overflow: hidden; text-overflow:ellipsis ...
- 未A,或用水法,或不熟的题
今天是2017.11.25 1. 用栈实现dfs JZOJ_senior 3467 2. 链表加堆或线段树乱搞 JZOJ_senior 3480 3. 求每个边所在的奇环.偶环 JZOJ_senior ...
- https://blog.csdn.net/u012235003/article/details/54576737
https://blog.csdn.net/u012235003/article/details/54576737