拓扑序……好些玄妙

Description

Mr. F. wants to get a document be signed by a minister. A minister signs a document only if it is approved by his ministry. The ministry is an M-floor building with floors numbered from 1 to M, 1<=M<=100. Each floor has N rooms (1<=N<=500) also numbered from 1 to N. In each room there is one (and only one) official. 
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

The first line of an input file contains two integers, separated by space. The first integer M represents the number of floors in the building, and the second integer N represents the number of rooms per floor. Each of the next M lines contains N integers separated with spaces that describe fees (the k-th integer at l-th line is the fee required by the official working in the k-th room at the l-th floor).

Output

You should print the numbers of rooms (one per line) in the order they should be visited to approve the document in the cheapest way. If there are more than one way leading to the cheapest cost you may print an any of them.

Sample Input

3 4
10 10 1 10
2 2 2 10
1 10 10 10

Sample Output

3
3
2
1
1

Hint

You can assume that for each official there always exists a way to get the approval of a document (from the 1st floor to this official inclusively) paying no more than 10^9. 
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的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. IT兄弟连 JavaWeb教程 JSP定义

    JSP页面是指扩展名为.jsp的文件,在一个JSP页面中,可以包括指令标识.HTML代码.JavaScript代码.嵌入的Java代码.注释和JSP动作标识等内容.但这些内容并不是一个JSP页面所必须 ...

  2. 如何在Linux服务器上部署禅道

    最近换了新的项目团队,由于新团队比较年轻化,没有实行正规的项目管理,于是我自告奋勇要为团队管理出一份力,帮助团队建立敏捷化的项目管理,经过多方考究和对比后,选择了目前较受欢迎的开源项目管理软件:禅道. ...

  3. StringUtils中常用方法leftPad(),rightPad(),center()

    org.apache.commons.lang3的StringUtils 方法如下: public static String leftPadTime(Integer time){    return ...

  4. assembly x86(nasm)串比较

    预留字符串口令,输入口令串与预留密码串比较.若匹配则显示“MATCH!CONGRATULATION”,否则显示“NOMATCH!”,并让用户重新输入,程序能对口令进行测试,但测试次数最多3次,若3次输 ...

  5. STP-4-每VLAN生成树和Trunk上的STP

    如果在有冗余链路且有多个VLAN的交换网络中只使用 STP实例,那么在稳定状态中,仍会有一些端口处于阻塞状态不被使用,冗余链路实际上变成了备份链路. PVST+特性能为每个VLAN创建一个STP实例. ...

  6. 【aspnetcore】让aspnetcore支持less文件

    第一步:新建文件 CustomerFileExtensionContentTypeProvider namespace xxx { public class CustomerFileExtension ...

  7. 2018百度之星初赛(A)2 度度熊学队列

    思路: 记录一下c++ stl中的双向链表list的各种用法. https://blog.csdn.net/fanyun_01/article/details/56881515 实现: #includ ...

  8. (转载)最近总是遇到各种 IEbug,mark一下,学习到了,转载出处:http://www.cnblogs.com/ruomeng/p/5332814.html

    本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几点都关注过,那么基本上很大一部分IE8+兼容性问题都OK ...

  9. arcgis jsapi接口入门系列(6):样式

    symbol: function () { //线样式 //样式详情请看官方文档 let style = { //线颜色,支持多种格式: //CSS color string:例如"dodg ...

  10. Android Studio报错Unable to resolve dependency for ':app@release/compileClasspath':无法引用任何外部依赖的解决办法

    Android Studio 在引用外部依赖时,发现一直无法引用外部依赖.刚开始以为是墙的问题,尝试修改Gradle配置,未解决问题. 最终发现原来是在Android Sudio安装优化配置时,将Gr ...