【动态规划】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 ...
随机推荐
- performSegueWithIdentifier 不生效的解决办法
相信很多人都会遇到这样的需求: APP 打开以后,判断用户是否登录,如果未登录,就跳转到登陆页. 今天我也遇到了这个需求,发现我封装的一个 `func checkLoginStatus()` 放在 ` ...
- echarts散点图重叠
今天做echarts图标,使用了散点图.很快实现,发现数据不对,应该是3个的企业,页面只显示了2个,查了半天才发现原来是有两个重叠了.想办法解决了,在网上费劲九牛二虎只力终于找到了解决的方法,下面来解 ...
- Python-1-基础
获取用户输入 >>> x = input("x: ") x: 34 >>> y = input("y: ") y: 42 & ...
- 转 shell模拟数据库的读写
0.create table create table myTestTable as select rownum as id, to_char(sysdate + rown ...
- python+selenium+requests爬取qq空间相册时遇到的问题及解决思路
最近研究了下用python爬取qq空间相册的问题,遇到的问题及解决思路如下: 1.qq空间相册的访问需要qq登录并且需是好友,requests模块模拟qq登录略显麻烦,所以采用selenium的dri ...
- (转载)Unity 优化总结
Unity 优化总结 2017-03-10 | 发布 大海明月 zengfeng75@qq.com | 分类 Unity | 标签 Unity 优化 相关文档 UGUI 降低填充率技巧两则 U ...
- nodejs express 设置html后缀模板
express 框架的默认渲染模板的后缀是 ejs ,由于编译器在ejs的文件里写html代码没有高亮显示,所以使用html模板. 示例: var app = express(); app.set(' ...
- 简单的鼠标经过特效-mouse事件
<!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- vuex2 10分钟快速入门
因为太简单了,我直接就贴代码了~ #建立store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex) export de ...
- eCharts基础知识
eCharts插件介绍 http://echarts.baidu.com/tutorial.html#ECharts%20%E7%89%B9%E6%80%A7%E4%BB%8B%E7%BB%8D