Ministry
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4220   Accepted: 1348   Special Judge

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
题目大意:有一栋m层的房子,每层n个房间,如果该房间在第一层或者该房间的上一层的房间或者左右任意一个房间被访问才能访问该房间,问从第一层到最后一层走过的房间的权值和最小的路径。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int fee[][];
int dp[][];
int path[][];
int ans[]; int main()
{
int m, n;
int s;
while(scanf("%d%d", &m, &n) != EOF)
{
memset(dp, , sizeof(dp));
memset(path, , sizeof(path));
memset(ans, , sizeof(ans));
for (int i = ; i <= m; i++)
{
for (int j = ; j <= n; j++)
{
scanf("%d", &fee[i][j]);
}
}
for (int i = ; i <= n; i++)
{
dp[][i] = fee[][i];
path[][i] = i;
}
for (int i = ; i <= m; i++)
{
dp[i][] = dp[i - ][] + fee[i][];
path[i][] = ;
for (int j = ; j <= n; j++)
{
if (dp[i - ][j] < dp[i][j - ])
{
dp[i][j] = fee[i][j] + dp[i - ][j];
path[i][j] = j;
}
else
{
dp[i][j] = fee[i][j] + dp[i][j- ];
path[i][j] = j - ;
}
}
for (int j = n - ; j > ; j--)
{
if (dp[i][j + ] + fee[i][j] < dp[i][j])
{
dp[i][j] = dp[i][j + ] + fee[i][j];
path[i][j] = j + ;
}
}
}
int temp = 0x7fffffff;
for (int i = ; i <= n; i++)
{
if (temp > dp[m][i])
{
temp = dp[m][i];
s = i;
}
}
int nCount = ;
int x = m;
ans[] = s;
while (x != )
{
nCount++;
ans[nCount] = path[x][ans[nCount - ]];
if (ans[nCount] == ans[nCount - ])
{
x--;
}
}
for (int i = nCount; i >=; i--)
{
printf("%d\n", ans[i]);
}
}
return ;
}

POJ 2353 Ministry的更多相关文章

  1. POJ 2353 DP

    双向DP+记录路径. // by SiriusRen #include <stack> #include <cstdio> #include <cstring> u ...

  2. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  3. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  4. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  5. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  6. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  7. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  8. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  9. DP题目列表/弟屁专题

    声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...

随机推荐

  1. UVA 624 CD(01背包,要记录路径)

    题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...

  2. 使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm

    Helm之于Kubernetes好比yum之于Red Hat Enterprise Linux,或者apt-get之于Ubuntu. Helm是由helm CLI和Tiller组成,是典型的Clien ...

  3. ABAP vs Java, 蛙泳 vs 自由泳

    去年7月定下的一年之内学会自由泳的目标终于实现了,特来还愿. ABAP和Java, 蛙泳和自由泳.前面的组合是Jerry用来挣钱养家的技术,后者是Jerry花了大量业余时间和金钱苦练的技能.或许有的朋 ...

  4. cv2.getPerspectiveTransform 透视变换

    简介 透视变换(Perspective Transformation)是将成像投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping).如图1,通过透 ...

  5. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep

    PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep ...

  6. HTML之基本语法(链接标签、路径的介绍和使用)

    一.链接标签 语法:<a href="目标地址">这个标签上展示的内容</a> 作用:可以实现在当前页面跳转到新页面的操作 属性 1.target这个属性可 ...

  7. 交叉熵cross entropy和相对熵(kl散度)

    交叉熵可在神经网络(机器学习)中作为损失函数,p表示真实标记的分布,q则为训练后的模型的预测标记分布,交叉熵损失函数可以衡量真实分布p与当前训练得到的概率分布q有多么大的差异. 相对熵(relativ ...

  8. webpack4 + vue多页面项目精细构建思路

    #构建思路 虽然当前前端项目多以单页面为主,但多页面也并非一无是处,在一些情况下也是有用武之地的,比如: 项目庞大,各个业务模块需要解耦 SEO更容易优化 没有复杂的状态管理问题 可以实现页面单独上线 ...

  9. Servlet的引入(即加入Servlet)

    今天讲的Servlet是根据上一章节<创建一个学生信息表,与页面分离>而结合. 一.看图分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuSer ...

  10. 01_3_创建一个Action

    01_3_创建一个Action 1. 定义一个action 具体视图的返回可以由用户自己定义的Action来决定 具体的手段是根据返回的字符串找到相应的配置项,来决定视图的内容 具体Action的实现 ...