UVA 116 Unidirectional TSP 经典dp题
题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小。
用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题的更多相关文章
- uva 116 Unidirectional TSP (DP)
uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...
- uva 116 Unidirectional TSP【号码塔+打印路径】
主题: uva 116 Unidirectional TSP 意甲冠军:给定一个矩阵,当前格儿童值三个方向回格最小值和当前的和,就第一列的最小值并打印路径(同样则去字典序最小的). 分析:刚開始想错了 ...
- 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 (DP)
该题是<算法竞赛入门经典(第二版)>的一道例题,难度不算大.我先在没看题解的情况下自己做了一遍,虽然最终通过了,思路与书上的也一样.但比书上的代码复杂了很多,可见自己对问题的处理还是有所欠 ...
- UVA - 116 Unidirectional TSP 多段图的最短路 dp
题意 略 分析 因为字典序最小,所以从后面的列递推,每次对上一列的三个方向的行排序就能确保,数字之和最小DP就完事了 代码 因为有个地方数组名next和里面本身的某个东西冲突了,所以编译错了,后来改成 ...
- UVa - 116 - Unidirectional TSP
Background Problems that require minimum paths through some domain appear in many different areas of ...
- uva 116 - Unidirectional TSP (动态规划)
第一次做动规题目,下面均为个人理解以及个人方法,状态转移方程以及状态的定义也是依据个人理解.请过路大神不吝赐教. 状态:每一列的每个数[ i ][ j ]都是一个状态: 然后定义状态[ i ][ j ...
- UVA - 116 Unidirectional TSP (单向TSP)(dp---多段图的最短路)
题意:给一个m行n列(m<=10, n<=100)的整数矩阵,从第一列任何一个位置出发每次往右,右上或右下走一格,最终到达最后一列.要求经过的整数之和最小.第一行的上一行是最后一行,最后一 ...
随机推荐
- wikioi1688 求逆序对
题目描述 Description 给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我们称之为逆序对,求逆序对的数目 数据范围:N<=105.Ai<=105. ...
- today reading notes
paminit manager from upstart to systemd/systemctl;Vivid Vervet + openStack kilo;为容器开发者(OpenStack工作环 ...
- MindManager_9.1.157使用模板时显示“参数错误”
每次使用标准模板时都出现这个问题,上网搜索,原来是模板中存在的 “注释”导致的问题.具体原因不详,解决起来也简单,就是繁琐一点.转抄如下: 先找到模板文件夹,共四个文件夹Communcation.Pe ...
- 我使用过的Linux命令之file - 检测并显示文件类型
摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...
- 学习本课程需要具备哪些基础及微信小程序目录结构介绍
1.html css js 基础 2.ajax 基础 3.简单的面向对象基础
- hdu 5570 balls(期望好题)
Problem Description There are n balls with m colors. The possibility of that the color of the i-th b ...
- 【转】zookeeper 的监控工具
公司很多产品会使用zookeeper,比如Meta消息中间件,在测试的过程中,我们经常需要查询zookeeper里面的信息来精确定位问题.目前项目中有开发团队自己写的浏览器node-z ...
- 查看oracle锁及解决办法
SQL> select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked_object t1, v$session t2 whe ...
- Web.config配置和节点介绍
Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...
- IBM WebSphere MQ的C#工具类以及源码(net)
简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...