POJ2353——Ministry
来源:互联网 发布:centos www服务器配置 编辑:程序博客网 时间:2024/06/05 17:35
Ministry
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4528 Accepted: 1452 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:
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.
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 410 10 1 102 2 2 101 10 10 10
Sample Output
33211
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.
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
Source
Ural Collegiate Programming Contest 1999
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i][j+1]) + mat[i][j]
方案可以递归输出
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i][j+1]) + mat[i][j]
方案可以递归输出
#include <map>#include <set>#include <list>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 510;const int M = 110;const int inf = 0x3f3f3f3f;int dp[M][N], mat[M][N];int path[M][N];int m, n, cnt;void print(int i, int j)/*第i层第j个房间*/{if (i == 1){return;}if (path[i][j] == j){print(i - 1, j);printf("%d\n", j);}else if (path[i][j] == j - 1){print(i, j - 1);printf("%d\n", j - 1);}else{print(i, j + 1);printf("%d\n", j + 1);}}int main(){while (~scanf("%d%d", &m, &n)){memset (dp, inf, sizeof(dp));for (int i = 1; i <= m; ++i){for (int j = 1; j <= n; ++j){scanf("%d", &mat[i][j]);}}for (int i = 1; i <= n; ++i){dp[1][i] = mat[1][i];path[1][i] = i;}for (int i = 2; i <= m; ++i){for (int j = 1; j <= n; ++j){dp[i][j] = mat[i][j] + dp[i - 1][j];path[i][j] = j;}for (int j = 2; j <= n; ++j){if (dp[i][j] > dp[i][j - 1] + mat[i][j]){dp[i][j] = dp[i][j - 1] + mat[i][j];path[i][j] = j - 1;}}for (int j = n - 1; j >= 1; --j){if (dp[i][j] > dp[i][j + 1] + mat[i][j]){dp[i][j] = dp[i][j + 1] + mat[i][j];path[i][j] = j + 1;}}}int ans = inf, s;cnt = 0;for (int i = 1; i <= n; ++i){if (ans > dp[m][i]){s = i;ans = dp[m][i];}}print(m, s);printf("%d\n", s);}return 0;}
0 0
- POJ2353——Ministry
- poj2353
- Ministry
- [DP] poj2353
- pku2353 Ministry
- poj Ministry
- B - Ministry
- poj2353 数装数组
- poj2353--双向DP
- ural 1029 Ministry
- ural 1029 Ministry
- POJ 2353 Ministry [DP]
- ural 1029. Ministry (dp)
- ural 1029. Ministry
- Timus 1029. Ministry
- poj 2353 Ministry
- POJ 2353 Ministry DP
- poj 2353 Ministry
- iOS coreText 图文混排
- Bundle类的作用
- PlaySound与CFileDialog
- 【DP经典问题】
- PhotoShop算法实现--直方图(十)
- POJ2353——Ministry
- 黑马程序员———基本数据类型对象包装类
- 机房收费系统合作版——VS2013自带报表
- HIVE 0.14.0安装
- 百灵面试
- 杭电1233还是畅通工程
- An introduction to Linux Virtual Address Space Layout
- Android ListView加载头视图后容易忽略的问题
- 解决关于request.getremoteaddr()方法返回的内容是ipv6的问题