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)的整数矩阵,从第一列任何一个位置出发每次往右,右上或右下走一格,最终到达最后一列.要求经过的整数之和最小.第一行的上一行是最后一行,最后一 ...
随机推荐
- sql server数据库实现保留指定位数小数的函数
有时候需要对一个特定的含有小数点的数字保留指定位数,比如"123.123600". 在数据库中以函数的形式实现如下: USE [数据库名称] GO /****** Object: ...
- nginx模块开发获取post参数
> 您好!> 我想请问下nginx模块里面怎么获取post参数,能有具体的代码更好!谢谢> 对于 "application/x-www-form-urlencode ...
- MFC子窗口和父窗口(SetParent,SetOwner)
一.概念和区别 在windows系统中,每个窗口对象都对应有一个数据结构,形成一个list链表.系统的窗口管理器通过这个list来获取窗口信息和管理每个窗口.这个数据结构中有四个数据用来构建list, ...
- Serv-U无法连接到服务器127.0.0.1,端口43958 FTP服务器不能启动
端口43958,这是Serv-U的本地管理端口,只允许127.0.0.1连接. 在出现“Serv-U无法连接到服务器127.0.0.1,端口43958”这个错误的时候,一般ftp软件无法自动启动 ...
- 一道google面试题(dp)
输入n,把1-n分成两个和相等的子集,有多少种分法 想了个dp,直接背包也行 #include <iostream> #include <cstdio> using names ...
- codeblocks 使用指南z
1.界面风格更改 首先贴怎么普通设置出来,或者改配置文件 这是我的风格 类似于DEV-CPP里面的一个主题,看的很舒服 具体设置如下: Settings-Editor 1.代码当前行高亮 在Gener ...
- 记一次dedeCMS网站搭建全过程
Step 1 使用阿里云Windows Server 2012服务器 { 使用远程桌面进行操作,ip admin pwd登录 } Step 2 下载安装phpStudy包 { 下载安装,直接安装到C盘 ...
- ExecutorService 的理解与使用
ExecutorService 的理解与使用 http://my.oschina.net/bairrfhoinn/blog/177639 Java线程池ExecutorService http:// ...
- HDU 1017 - A Mathematical Curiosity
题目简单,格式酸爽.. #include <iostream> using namespace std; int ans,n,m,p; int main() { cin>>p; ...
- BZOJ 4008: [HNOI2015]亚瑟王( dp )
dp(i, j)表示考虑了前i张牌, 然后还有j轮的概率. 考虑第i+1张牌: 发动的概率 : p = dp(i, j) * (1 - (1-p[i+1])^j) 没发动的概率 : dp(i, j) ...