【动态规划】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 ...
随机推荐
- IT兄弟连 JavaWeb教程 JSP定义
JSP页面是指扩展名为.jsp的文件,在一个JSP页面中,可以包括指令标识.HTML代码.JavaScript代码.嵌入的Java代码.注释和JSP动作标识等内容.但这些内容并不是一个JSP页面所必须 ...
- 跳转到另一个APP
看看这个代码: http://code4app.com/codesample/4fcc512d6803fae60b000002 inApp跳转,不过需要Nimbus类库. 要跳转到另一个APP,需要另 ...
- 下载devc++和codeblocks记录
dev的安装包自己百度网盘里有 codeblocks官网 下载好后再解压即可,如果不是默认路径安装的话,还会出现检测不到编译器路径问题,解决办法在这.
- swift SqliteDB使用
操作步骤: 1,在 Build Phases -> Link Binary With Libraries 中点击加号,添加 libsqlite3.0.tbd 到项目中来 2,创建连接头文件B ...
- 两句话跳转QQ聊天界面 通过web方式
NSString *qq=[NSString stringWithFormat:"]; NSURL *url2 = [NSURL URLWithString:qq]; if ([[UIApp ...
- 最短路之SPFA(单源)HDU 2544
#include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...
- CentOS7下使用Docker容器化.net Core 2.2
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...
- 接口测试02 - 无法绕过的json解析
概述: 先瞧一下什么是json.JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式. 它基于ECMAScript(w3c定制的js规范)的一个子集 ...
- [在读]JavaScript异步编程:设计快速响应的网络应用
很棒的一本,就如书名所示,主要讲js异步的一些东西,比如定时器.Jquery的promise和deffered,node...看了一小半.推荐哦~~
- 求N之下的所有素数
No.1 f=lambda n: [x for x in range(1,n) if not [y for y in range(2,x) if x%y ==0 ]] No.2 def prime(n ...