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. hdu 3861 The King’s Problem

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. 在2017年,如何将你的小米4刷上Windows 10 mobile?(后附大量图赏)

    众多攻略集大成者!资深软粉亲测有效! 参考教程: http://bbs.xiaomi.cn/t-11814358 http://bbs.xiaomi.cn/t-11736827 问:刷机前,我需要做什 ...

  3. BZOJ 2851: 极限满月 虚树 or 树链的并

    2851: 极限满月 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 170  Solved: 82[Submit][Status][Discuss] ...

  4. thinkphp 跳转

    1   $this -> redirect('index',array('type'=>2,'id'=>0));   //直接跳转 2  $this->success('提交失 ...

  5. MySql查询时间段的方法

    本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面为您介绍两种MySql查询时间段的方法,供大家参考. MySql的时间字段有 ...

  6. mysq--索引模块

    问题:为什么要 使用索引? --->快速查询数据,但是仅仅这么回答,就是不专业的!!! 应该要分为数据量少的时候,不适用索引,走全表扫描的话,查询速率也是很快的 数据量大的话,使用索引,查询速率 ...

  7. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  8. ubuntu 16.04 连接 wifi

    我的电脑是win10+ubuntu16.04双系统.在ubuntu下无法连接wifi,一直用usb连接的手机流量,不太方便.现在来用安装无线驱动,顺便翻个墙. https://blog.csdn.ne ...

  9. HDU-4848-Such Conquering

    这题就是深搜加剪枝,有一个很明显的剪枝,因为题目中给出了一个deadline,所以我们一定要用这个deadline来进行剪枝. 题目的意思是求到达每个点的时间总和,当时把题看错了,卡了好久. 剪枝一: ...

  10. 不依赖Hibernate的万能BaseDao---模仿了Hibernate底层的原理

    今天写了个万能的BaseDao:有了这个BaseDao以后的Dao层直接继承这个BaseDao就能直接操作数据库了,增删改查,这是一个简易的Hibernate模型.写这个BaseDao的原因是最近在学 ...