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的更多相关文章

  1. 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], ...

  2. UVA116 Unidirectional TSP 单向TSP

    分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...

  3. HDU 1619 Unidirectional TSP(单向TSP + 路径打印)

    Unidirectional TSP Problem Description Problems that require minimum paths through some domain appea ...

  4. uva 116 Unidirectional TSP (DP)

    uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...

  5. UVA 116 Unidirectional TSP(dp + 数塔问题)

     Unidirectional TSP  Background Problems that require minimum paths through some domain appear in ma ...

  6. UVA 116 Unidirectional TSP(DP最短路字典序)

    Description    Unidirectional TSP  Background Problems that require minimum paths through some domai ...

  7. uva 116 Unidirectional TSP【号码塔+打印路径】

    主题: uva 116 Unidirectional TSP 意甲冠军:给定一个矩阵,当前格儿童值三个方向回格最小值和当前的和,就第一列的最小值并打印路径(同样则去字典序最小的). 分析:刚開始想错了 ...

  8. 9-4 Unidirectional TSP uva116 (DP)

    题意:给一个n行m列矩阵    从第一列任意一个位置出发 每次往右 右上 右下三个方向走一格 直到最后一列   输出所类和的最小值和路径!! 最小值相同则输出字典序最小路径 很像一开始介绍的三角形dp ...

  9. UVA 116 Unidirectional TSP 经典dp题

    题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小. 用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...

随机推荐

  1. c语言学习笔记 - 结构体位域

    在学习结构体的时候遇到了位域这个概念,位域主要是为了节省内存空间,比如用一个32位,4个字节的int存储一个开关变量时,会造成空间浪费,于是干脆就考虑在这个32划分不同的区域来存储数据,例如划出1位存 ...

  2. lavarel中如何使用memcache

    lavarel中如何使用memcache 一.总结 一句话总结: composer下载包,配置,使用函数 1.memcache是什么? 键值对内存缓存 MemCache是一个自由.源码开放.高性能.分 ...

  3. day 45 前端CSS

      前端CSS   CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素,给HTML设置样式,让它更加美观. 当浏览器读到一个样式表,它就会按照这个样式 ...

  4. 《DSP using MATLAB》Problem 8.15

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  5. 2018-8-10-WPF-鼠标移动到列表上-显示列表图标

    title author date CreateTime categories WPF 鼠标移动到列表上 显示列表图标 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

  6. tcpdump 抓包

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  7. Css if hack条件语法

    Css if hack条件语法  <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> ...

  8. c++容器的操作方法总结

    一.map 1.创建 typedef map<int,string> descrbe_map_; descrbe_map_ devMap; 或者 map<string,string& ...

  9. 获取地址栏URL中参数, getQuerySting()方法

    今天同事用的以前的获取url地址参数获取不到.以前的方法失效了.后面发现是正则表达式bug: 第一种获取方法(针对普通情况的一般够用): function getQueryString(name) { ...

  10. php filemtime filectime fileatime的区别

    1.fileatime()int fileatime(string filename):fileatime()函数返回filename最后访问的时间,这里的最后访问是指每当一个文件的数据块被读取,采用 ...