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. 【Troubleshooting Case】Exchange Server 组件状态应用排错?

    在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...

  2. JavaSe-算数运算符

    算数运算符包括:+.-.*./.%.++.-- 代码: package com.java.chap02; public class Demo07 { public static void main(S ...

  3. Cause: java.lang.UnsupportedOperationException

    运行web项目的时候出现以下错误: ### Cause: java.lang.UnsupportedOperationException    at org.mybatis.spring.MyBati ...

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

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

  5. Nuget~管理自己的包包

    很久很久以前,自己就想有个包包,最近又从网上淘了一个,价格不便宜呢,99块,还是个小腰包,不过作工还算精良,我喜欢的类型,帆布休闲包,可以将我的手机,耳机,水,小烟,小酒,小伞都放里,方便至极,哈哈!

  6. python基础一 day15 内置函数

    '\r' 回车,回到当前行的行首,而不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖: '\n' 换行,换到当前位置的下一行,而不会回到行首: # print()# input()# le ...

  7. 2018.2.25 关于JavaScript

    关于JavaScript 1.数组的归约函数reduce(function(PREV,CUR,I){})会从做导游进行迭代,每次返回的值为下一次的prev参数. 2.在循环遍历数组时若是想在找到结果后 ...

  8. MFC多文档无法显示可停靠窗格

    当我们使用MFC多文档创建项目时,我们可停靠窗格关闭之后就无法显示了.即使重新编译项目也无法再次显示它们. 原因:因为MFC多文档把这些设置存储在注册表 “HKEY_CURRENT_USER \ SO ...

  9. Ajax的原理及Django上传组件

    title: Ajax的原理及Django上传组件 tags: Django --- Ajax的原理及Django上传组件 Ajax的原理 ajax 是异步JavaScript和xml ajax就是向 ...

  10. mongo 副本集+分片 配置

    服务器规划如下: 副本集名称|服务器IP 192.168.56.111 192.168.56.112 192.168.56.113 shard1 3201 3201 3201 shard2 3202 ...