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记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...
随机推荐
- JavaSE_12_序列化流和打印流
1.1 序列化流概述 Java 提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存 ...
- js图片预加载实现!
var myImage = (function(){ var imgNode = document.createElement( 'img' ); document.body.appendChild( ...
- 深入浅出 Java Concurrency (25): 并发容器 part 10 双向并发阻塞队列 BlockingDeque[转]
这个小节介绍Queue的最后一个工具,也是最强大的一个工具.从名称上就可以看到此工具的特点:双向并发阻塞队列.所谓双向是指可以从队列的头和尾同时操作,并发只是线程安全的实现,阻塞允许在入队出队不满足条 ...
- ubuntu挂载和挂载NTFS分区命令
1.挂载 首先安装NTFS-3g,不过Ubuntu一般自带: sudo apt install ntfs-3g 查看分区信息: sudo fdisk -l 结果类似: /dev/sda1 * ...
- 转-Pycharm中运行Python代码的几种方式
转自:Pycharm中运行Python代码的几种方式 在pycharm中的Python代码运行会出现各种奇葩的问题,比如,密码输入时不显示或没有提示,给我们带来一些麻烦,下面介绍几种代码运行的几种方式 ...
- Mac OS X 下有关adb相关问题
一.什么是adb? ADB的全称是Android Debug Bridge,用来调试Android程序的,白话点就是debug工具! 位置:一般下载Android的SDK时候在platform-too ...
- Undertow服务器基础分析 - XNIO
阅读更多 我们从名字上就能看出这是一个NIO思想为基础的IO框架,X是指这个框架可以有多种实现,我们可以从代码库 https://github.com/xnio 中发现一个项目xnio-native, ...
- Linux常用知识
1.Redhat 系统按如下系统启动:加载内核执行init程序/etc/rc.d/rc.sysinit #由init执行的第一个脚本/etc/rc.d/rc${RUNLEVEL}d/* #$RUNLE ...
- 跟我一起了解koa之koa洋葱圈模型(二)
根据上篇博文的博文,继续接下来我们实现的洋葱圈模型的实现 新建middleware文件夹 //m1.js function m1(ctx){ global.console.log('m1') } mo ...
- SyntaxError: Non-ASCII character ‘xe5’ in file 04.py on line 4, but no encoding declared
出现问题的原因:程序中的编码错误,python默认是acii模式,没有支持utf8,代码中需要输出汉字,所以报错. 解决办法:源代码文件第一行添加:#coding:utf-8 -- coding: U ...