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. mysql-新增、更新、删除语句

    1.插入数据: INSERT INTO t_book VALUES(NULL,'我爱我家',20,'张三',1); INSERT INTO t_book(id,bookName,price,autho ...

  2. COGS 2794. 爱摔跤的比利海灵顿

    ★☆   输入文件:find_k.in   输出文件:find_k.out   简单对比时间限制:1.4 s   内存限制:128 MB [题目描述] B•海灵顿•雷想要和n个巨人比试摔♂跤,他想先和 ...

  3. UWP开发:自动生成迷宫&自动寻路算法(1)

    (1)前端篇 首先,我们创建一个新的Universal Windows Platform程序.这些小方块是通过GridView来罗列的,这样可以避免MainPaga.xaml的<Rectangl ...

  4. 如何处理VirtualBox启动错误消息:The vboxdrv kernel module is not loaded

    我在启动minikube时,遇到如下错误消息: Starting local Kubernetes v1.10.0 cluster... Starting VM... E1010 03:27:37.9 ...

  5. 日常-acm-子序列的和

    输入两个正整数n<m<10^6,输出,保留五位小数.输入包含多组数据,结束标记为n=m=0. 样例输入: 2 4 65536 655360 0 0 样例输出: Case 1:0.42361 ...

  6. 101个MySQL的调节和优化技巧

    MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些思 ...

  7. [VC]listctrl的基本用法

    1   添加listctrl的头 m_list.setextendedstyle(LVS_EX_FULLROWSELECT||LVS_EX_GRIdLINES); m_list.insertcolum ...

  8. CoreData介绍

    http://blog.csdn.net/zh952016281/article/details/52105683 写在前面 在CoreData中有一些常用的类,称呼可能各不相同.所以这里先约定一些关 ...

  9. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  10. php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分

    我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...