【动态规划】poj2353Ministry
拓扑序……好些玄妙
Description
A document is approved by the ministry only if it is signed by at least one official from the M-th floor. An official signs a document only if at least one of the following conditions is satisfied:
a. the official works on the 1st floor;
b. the document is signed by the official working in the room with the same number but situated one floor below;
c. the document is signed by an official working in a neighbouring room (rooms are neighbouring if they are situated on the same floor and their numbers differ by one).
Each official collects a fee for signing a document. The fee is a positive integer not exceeding 10^9.
You should find the cheapest way to approve the document.
Input
Output
Sample Input
3 4
10 10 1 10
2 2 2 10
1 10 10 10
Sample Output
3
3
2
1
1
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
题目大意
有一个带权矩阵,可以从上面任意一点进入,从下面任意一点走出;问路径上权值和的最小值。
题目分析
题目很简单,就是普通的dp做两次……
只不过想记录一下这个dp拓扑序的问题。
对于点$(x,y)$需要先从上面转移,再从两边转移。虽然看上去随便怎么样好像都一样、会根据最优解覆盖,但是实际上是要考虑这个dp的拓扑序的……
=
#pragma GCC optimize(2)
#include<cstring>
#include<cctype>
#include<cstdio>
const int maxn = ; int f[maxn][maxn],a[maxn][maxn];
int n,m,g[maxn][maxn],cnt; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void dfs(int layer, int x)
{
if (layer!=&&!g[layer][x]) dfs(layer-, x);
else if (g[layer][x]) dfs(layer, x+g[layer][x]);
printf("%d\n",x);
}
int main()
{
register int i,j,tt = ;
n = read(), m = read();
for (i=; i<=n; i++)
for (j=; j<=m; j++)
a[i][j] = read(), f[i][j] = 2e9;
f[n][] = 2e9;
for (i=; i<=m; i++)
f[][i] = a[][i];
for (i=; i<=n; i++)
{
for (j=; j<=m; j++)
{
if (f[i][j] > f[i-][j]+a[i][j]){
f[i][j] = f[i-][j]+a[i][j];
g[i][j] = ;
}
if (j!=&&f[i][j] > f[i][j-]+a[i][j]){
f[i][j] = f[i][j-]+a[i][j];
g[i][j] = -;
}
}
for (j=m-; j>=; j--)
{
if (f[i][j] > f[i][j+]+a[i][j]){
f[i][j] = f[i][j+]+a[i][j];
g[i][j] = ;
}
}
}
for (i=; i<=m; i++)
if (f[n][tt] > f[n][i]) tt = i;
dfs(n, tt);
return ;
}
END
【动态规划】poj2353Ministry的更多相关文章
- 增强学习(三)----- MDP的动态规划解法
上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- C#动态规划查找两个字符串最大子串
//动态规划查找两个字符串最大子串 public static string lcs(string word1, string word2) { ...
- C#递归、动态规划计算斐波那契数列
//递归 public static long recurFib(int num) { if (num < 2) ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- 搭建 CDH 版本hive
搭建一个完整的cdh 的版本,由于涉及的产品和步骤太多,在客户那里部署环境时,很容易出现意外,所以如果只是需要部署一个测试环境来进行验证,我们没有必要完完整整的部署整个cdh. 下面是通过命令行的 ...
- Mybatis分页中遇到的坑3
Mybatis Mapper.xml 配置文件中 resultMap 节点的源码解析 相关文章 Mybatis 解析配置文件的源码解析 Mybatis 类型转换源码分析 Mybatis 数据源和数 ...
- C 语言实例 - 字符串复制
C 语言实例 - 字符串复制 C 语言实例 C 语言实例 将一个变量的字符串复制到另外一个变量中. 实例 - 使用 strcpy() #include <stdio.h> #include ...
- StringUtils.split()和string.split()的区别
场景 出于业务考虑,将多个字符串拼接起来时,使用的分隔符是;,;.如果要将这样一个拼接来的字符串分割成原本的多个字符串时,就需要使用到jdk自带的split()方法.不过因为公司的编程规范,改为使用了 ...
- JSP | 基础 | 连接数据库
package util; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.*; ...
- POJ-1181-食物链
链接:https://vjudge.net/problem/POJ-1182 题意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...
- p标签中的文本换行
参考文章 word-break:break-all和word-wrap:break-word的区别 CSS自动换行.强制不换行.强制断行.超出显示省略号 属性介绍 white-space: 如何处理元 ...
- python入门之三元运算,存址方式,深浅拷贝
三元运算 格式: name = 值1 if 条件 else 值2 如果条件为True,那么将值1赋值给name,条件为False,那么将值2赋值给name 存址方式 不同的数据类型在内存中的存址方式不 ...
- 转 在shell脚本中使用expect实现scp传输问题
1.安装expect expect用于shell脚本中自动交互,其是基于tcl编程语言的工具.所以安装expect首先安装tcl.本文中使用的是expect5.45和tcl8.6.6. 安装tcl [ ...
- freertos之资源管理学习
OS下在对硬件外设资源操作.多任务的共享变量.任务和中断的共享变量操作时需要考虑资源的完整性和安全性. FREERTOS提供了临界区.调度器上锁.互斥量.优先级自动继承.创建守护任务的方法来改变最小优 ...