题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小。

用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路。

因为左边到右边的字典序最小就必须从左边开始找,于是我们可以换个思路,dp时从右边走到左边,这样寻找路径就可以从左向右了。

代码:

/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva116.cpp
* Create Date: 2013-09-20 20:56:07
* Descripton: dp, memorial
*/ #include <cstdio>
#include <algorithm>
using namespace std; const int MAXN = 102;
int dis[MAXN][MAXN], map[MAXN][MAXN], n, m; int cg(int x) {
if (x == 0) x = n;
else if (x == n + 1) x = 1;
return x;
} int dp(int x, int y) {
x = cg(x);
if (dis[x][y] != -0xffffff) return dis[x][y];
return dis[x][y] = map[x][y] + min(min(dp(x - 1, y + 1), dp(x, y + 1)), dp(x + 1, y + 1));
} void print(int x, int y) {
if (y < m)
printf("%d ", x);
else {
printf("%d\n", x);
return;
}
int a[3] = {cg(x - 1), cg(x), cg(x + 1)};
sort(a, a + 3);
int tt = dis[x][y] - map[x][y];
if (tt == dis[a[0]][y + 1])
print(a[0], y + 1);
else if (tt == dis[a[1]][y + 1])
print(a[1], y + 1);
else
print(a[2], y + 1);
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &map[i][j]);
if (j == m) dis[i][j] = map[i][j];
else dis[i][j] = -0xffffff;
}
int Min = 0xffffff, t, rx, ry;
for (int i = 1; i <= n; i++) {
t = dp(i, 1);
if (t < Min)
rx = i, Min = t;
}
print(rx, 1);
printf("%d\n", Min);
}
return 0;
}

UVA 116 Unidirectional TSP 经典dp题的更多相关文章

  1. uva 116 Unidirectional TSP (DP)

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

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

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

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

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

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

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

  5. UVa 116 Unidirectional TSP (DP)

    该题是<算法竞赛入门经典(第二版)>的一道例题,难度不算大.我先在没看题解的情况下自己做了一遍,虽然最终通过了,思路与书上的也一样.但比书上的代码复杂了很多,可见自己对问题的处理还是有所欠 ...

  6. UVA - 116 Unidirectional TSP 多段图的最短路 dp

    题意 略 分析 因为字典序最小,所以从后面的列递推,每次对上一列的三个方向的行排序就能确保,数字之和最小DP就完事了 代码 因为有个地方数组名next和里面本身的某个东西冲突了,所以编译错了,后来改成 ...

  7. UVa - 116 - Unidirectional TSP

    Background Problems that require minimum paths through some domain appear in many different areas of ...

  8. uva 116 - Unidirectional TSP (动态规划)

    第一次做动规题目,下面均为个人理解以及个人方法,状态转移方程以及状态的定义也是依据个人理解.请过路大神不吝赐教. 状态:每一列的每个数[ i ][ j ]都是一个状态: 然后定义状态[ i ][ j ...

  9. UVA - 116 Unidirectional TSP (单向TSP)(dp---多段图的最短路)

    题意:给一个m行n列(m<=10, n<=100)的整数矩阵,从第一列任何一个位置出发每次往右,右上或右下走一格,最终到达最后一列.要求经过的整数之和最小.第一行的上一行是最后一行,最后一 ...

随机推荐

  1. 补丁惹的祸-ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出 问题: 重新安装了VS2012,结 ...

  2. Can someone explain Webpack's CommonsChunkPlugin

    I get the general gist that the CommonsChunkPlugin looks at all the entry points, checks to see if t ...

  3. Oracle EBS-SQL (INV-9):检查搬运单分配异常.sql

    select h.request_number,         l.line_number,         msib.segment1 item_code,         t.transacti ...

  4. 致终将火爆的NFC——ISO14443 TypeA

    毫无疑问,当NFC终端越来越普及,逐渐成为智能手机标配功能后,我们终将迎来NFC的火爆.国内NFC应用最为广泛的将是TypeA,如Mifare.NFC Tag.移动支付等,所以接下来将主要研究Type ...

  5. (六)boost库之内存管理shared_ptr

    (六)boost库之内存管理shared_ptr 1.shared_ptr的基本用法 boost::shared_ptr<int> sp(new int(10)); //一个指向整数的sh ...

  6. cf467A George and Accommodation

    A. George and Accommodation time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Node中npm 安装问题

    首先,我们的npm包无所谓安全性,所以不要使用性能和效率更慢的https,转而使用http,相关命令如下: 1.关闭npm的https   npm config set strict-ssl fals ...

  8. Exception in thread "main" java.io.IOException: Failed to set permissions of path

    在跑BuildForest的时候,编写了下面的程序: package test.breiman; import org.apache.mahout.classifier.df.mapreduce.Bu ...

  9. 命名空间“System.Windows.Forms”中不存在类型或命名空间名称“DataVisualization”。是否缺少程序集引用?

    using System.Windows.Forms.DataVisualization.Charting; 编译时报警:命名空间"System.Windows.Forms"中不存 ...

  10. SVN连接不上

    某次我们部门换了场地.SVNserver就连接不上了,后来公司数据中心处理好后,还是连接不上,原来还需刷新自己电脑的DNS. 如需转载,请注明出处http://blog.csdn.net/combat ...