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记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...
随机推荐
- 模板——Treap
不得不说平衡树博大精深,除了Treap,还有splay,非旋Treap和可持久化数据结构,今天先讲讲Treap,也很感谢这位大佬的博客给予我帮助:http://www.360doc.com/conte ...
- final关键字与类型转换
一.关于final的重要知识点; 1.final关键字可以用于成员变量.本地变量.方法以及类. 2. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. 3. 你不能够 ...
- Mybatis-构建 SqlSessionFactory
从 XML 中构建 SqlSessionFactory 每 一 个 MyBatis 的 应 用 程 序 都 以 一 个 SqlSessionFactory 对 象 的 实 例 为 核 心 . SqlS ...
- Mybatis编写配置文件时,需要注意配置节点的顺序
mybatis-config.xml配置文件配置时,要注意节点顺序 <properties>...</properties> <settings>...</s ...
- Sublime Text3添加到右键快捷菜单教程(亲测可用)
前言: 安装Sublime Text3时,未选择右键快捷方式,可以通过下面的方式解决! 教程: 方法一: 新建sublime_addright.reg文件 编辑后双击打开就OK 注意:括号内是subl ...
- hdu6277
hdu6277结论题 #include<iostream> #include<cstdio> #include<queue> #include<algorit ...
- mysql导出某张表的部分数据
.使用into outfile '保存到操作系统的外部文件路径' mysql -uroot -p123456 -hhostname -P3306 select column_name_list fro ...
- Python基础——使用with结构打开多个文件
考虑如下的案例: 同时打开三个文件,文件行数一样,要求实现每个文件依次读取一行,然后输出,我们先来看比较容易想到的写法: with open(filename1, 'rb') as fp1: with ...
- 原生js封装ajax代码
方法一:(类似jQuery的封装方法) 1.ajax函数封装: /* *author: Ivan *date: 2014.06.01 *参数说明: *opts: {'可选参数'} **method: ...
- Linux跨PC拷贝之SCP
命令:scp 不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy. ...