Ministry

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 4761 Accepted: 1528 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

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时,要分从左到右和从右到左两种情况

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int a[105][505];
int dp[105][505];
int b[105][505];
int n,m;
int res[50005];
int main()
{
int ans;
int MAX=pow(10.0,9.0)*2;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(res,0,sizeof(res));
memset(b,0,sizeof(b));
ans=MAX;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
dp[i][j]=MAX;
b[i][j]=j;
}
}
for(int i=1;i<=m;i++)
dp[1][i]=a[1][i];
for(int i=2;i<n;i++)
{
int term;
dp[i][1]=dp[i-1][1]+a[i][1];
for(int j=2;j<=m;j++)
{
if(dp[i-1][j]>dp[i][j-1])
b[i][j]=j-1;
dp[i][j]=min(dp[i-1][j],dp[i][j-1])+a[i][j];
}
term=dp[i][m];
dp[i][m]=dp[i-1][m]+a[i][m];
for(int j=m-1;j>=1;j--)
{
if(dp[i][j+1]+a[i][j]<dp[i][j])
b[i][j]=j+1;
dp[i][j]=min(dp[i][j],dp[i][j+1]+a[i][j]);
}
dp[i][m]=min(dp[i][m],term);
if(term<dp[i][m])
b[i][m]=m-1;
}
int num=0;
for(int i=1;i<=m;i++)
{
if(ans>dp[n-1][i]+a[n][i])
{
ans=dp[n-1][i]+a[n][i];
num=i;
}
}
res[0]=num;
int tag=n;
int cot=0;
while(tag>1)
{
res[cot+1]=b[tag][res[cot]];
if(b[tag][res[cot]]==res[cot])
tag--;
cot++;
}
for(int i=cot;i>=0;i--)
printf("%d\n",res[i]); }
return 0;
}

POJ-2353 Ministry(动态规划)的更多相关文章

  1. POJ 2353 Ministry

    Ministry Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4220   Accepted: 1348   Specia ...

  2. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  3. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  4. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  5. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  6. POJ 1088 滑雪 -- 动态规划

    题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  7. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  8. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  9. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

  10. HOJ 2124 &POJ 2663Tri Tiling(动态规划)

    Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9016 Accepted: 4684 Descriptio ...

随机推荐

  1. linux环境下pytesseract的安装和央行征信中心的登录验证码识别

    首先是安装,我参考的是这个 http://blog.csdn.net/xinghun_4/article/details/47860645 我是centos,使用yum yum install pyt ...

  2. 如何用一个for循环打印出一个二维数组

    思路分析: 二维数组在内存中默认是按照行存储的,比如一个二维数组{{1,2,3,},{4,5,6}},它在内存中存储的顺序就是1.2.3.4.5.6,也就是说,对于这6个数组元素,按照从0到5给它们编 ...

  3. .NET Framework 4.0源代码

    原文出处:http://blogs.microsoft.co.il/blogs/arik/archive/2010/07/12/step-into-net-framework-4-0-source-c ...

  4. css背景图等比例缩放,盒子随背景图等比例缩放

    很多时候我们给网站了一个大banner,但是随着屏幕的变化,背景会变形,我们知道background-size可以实现背景图等比例缩放,但是,我们想让下面的盒子根据缩放后背景图的高度,也能自动向上挤. ...

  5. wee hours

    前言: 程序员问科比:“你为什么这么成功? ” 科比:“你知道凌晨四点的城市是什么样子吗?” 程序员:“知道,一般那个时候我还在写代码,怎么了?” 科比:“没事了……” 说起程序员,可能很多人脑中会蹦 ...

  6. .Net使用DES加密,.Net和java分别解密,并正则匹配替换加密密码为明文

    在VS中用WindowsApplication做一个exe程序,用来给数据库密码加密,加密代码如下 private void generateBtn_Click(object sender, Even ...

  7. 接口测试之JMeter初探

    1.JMeter安装配置 )登录 http://jmeter.apache.org/download_jmeter.cgi ,下载与自己的平台相对应文件: )安装JDK(.6以上),配置环境变量JAV ...

  8. ASP.NET MVC入门到精通——数据库仓储

    业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用. 1.在IDAL项目中,新建IDBSession.tt模板   Ctrl+S后自动生成IDBS ...

  9. [SublimeText] 如何创建工程

    Sublime Text 可以很方便地管理多个工程.使用Sublime Text的Projects,可以将不同根目录的文件组织起来成为一个工程,而不用将所有的文件都放到一个根目录下面. 1. 创建工程 ...

  10. ScrollView 定位

    1.固定到顶部scroll_view.requestFocusFromTouch();scroll_view.setScrollX(0); 2.固定到指定位置 int[] location = new ...