POJ2353Ministry题解动态规划DP

Ministry
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2849   Accepted: 864   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.

Source

Ural Collegiate Programming Contest 1999
状态:
设d[i][j]表示从第m层到第i层j个房间的最小花费。
pre[i][j]记录路径
状态转移方程:
d[i][j]=min{d[i-1][j],d[i][j-1],d[i][j+1]}+a[i][j];
双向DP
先从左到右DP一次,再从右向左DP一次
pre[i][j]: j-1 ,j ,j+1 分别表示左边,上层,右边的房间
边界:
d[m][i]=a[m][i]
代码:
#include<cstdio> #include<cstring> int m,n,p,s[105][505],d[105][505],pre[105][505]; int main() { int i,j; scanf("%d%d",&m,&n); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%d",&s[i][j]); for(i=m;i>0;i--) { for(j=1;j<=n;j++) { d[i][j]=d[i+1][j]+s[i][pre[i][j]=j]; if(j>1&&d[i][j-1]+s[i][j]<d[i][j]) d[i][j]=d[i][pre[i][j]=j-1]+s[i][j]; } for(j=n-1;j>0;j--) if(j<n&&d[i][j+1]+s[i][j]<d[i][j]) d[i][j]=d[i][pre[i][j]=j+1]+s[i][j]; } d[1][p]=1999999999; for(i=j=1;j<=n;j++) if(d[1][j]<d[1][p]) p=j; while(i<=m) { printf("%d/n",p); if(pre[i][p]==p) i++; else p=pre[i][p]; } }

你可能感兴趣的:(POJ2353Ministry题解动态规划DP)