动态规划

POJ2353总结

Ministry

题目来源

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.

思路

由于可以选择的方向有向上,向右,向左三种,其中向左和向右是相反的方向,需要进行两次dp.只需要进行两次dp的缘由是每一趟dp,dp[i]的产生都是依据前一项dp[i-1]已经被更新过了:

for (int i=1;i<m;i++){
                dp[i][0]=dp[i-1][0]+fee[i][0];
		for (int j=1;j<n;j++){//not 0..n-2
			dp[i][j]=min(dp[i-1][j],dp[i][j-1])+fee[i][j];
		}
		for (int j=n-2;j>=0;j--){
			dp[i][j]=min(dp[i][j],dp[i][j+1]+fee[i][j]);
		}
	}

另外初始化时,每一层的第一个房间的初始化,并不是在循环开始之前就执行的,而是在循环进行中执行的,否则会错.

//	for (int i=1;i<m;i++){
//		dp[i][0]=dp[i-1][0]+fee[i][0];
//	}
	for (int i=1;i<m;i++){
	dp[i][0]=dp[i-1][0]+fee[i][0];
		for (int j=1;j<n;j++){//not 0..n-2
			dp[i][j]=min(dp[i-1][j],dp[i][j-1])+fee[i][j];
		}
		for (int j=n-2;j>=0;j--){
			dp[i][j]=min(dp[i][j],dp[i][j+1]+fee[i][j]);
		}
	}
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;

int m,n;
int fee[110][510];
int dp[110][510];
stack<int> s;

int main(){
	int mini=0x3f3f3f3f;
	int pos=0;

	int level;
	memset(fee,0,sizeof(fee));
	memset(dp,0x3f3f3f3f,sizeof(dp));
	scanf("%d%d",&m,&n);
	for (int i=0;i<m;i++){
		for (int j=0;j<n;j++){
			scanf("%d",&fee[i][j]);
		}
	}
	for (int j=0;j<n;j++){
		dp[0][j]=fee[0][j];
	}
//	for (int i=1;i<m;i++){
//		dp[i][0]=dp[i-1][0]+fee[i][0];
//	}
	for (int i=1;i<m;i++){
	dp[i][0]=dp[i-1][0]+fee[i][0];
		for (int j=1;j<n;j++){//not 0..n-2
			dp[i][j]=min(dp[i-1][j],dp[i][j-1])+fee[i][j];
		}
		for (int j=n-2;j>=0;j--){
			dp[i][j]=min(dp[i][j],dp[i][j+1]+fee[i][j]);
		}
	}
//	for (int i=0;i<m;i++){
//		for (int j=0;j<n;j++){
//			printf("%4.d",dp[i][j]);
//		}
//		printf("\n");
//	}
	for (int j=0;j<n;j++){
		if (dp[m-1][j]<mini){
			mini=dp[m-1][j];
			pos=j;
		}
	}
	s.push(pos);
	level=m-1;
	int flag=-1;//-1:all,1:not left,2:not right
	while (level>0){
		if (dp[level][pos]-fee[level][pos]==dp[level-1][pos]){
			s.push(pos);
			level--;
			flag=-1;
		}else if (flag!=2&&pos>0&&dp[level][pos]-fee[level][pos]==dp[level][pos-1]){
			pos--;
			s.push(pos);
			flag=1;
		}else if (flag!=1&&pos<n-1&&dp[level][pos]-fee[level][pos]==dp[level][pos+1]){
			pos++;
			s.push(pos);
			flag=2;
		}else {
			printf("now:%d,left:%d,right:%d,down:%d,fee%d\n",dp[level][pos],dp[level][pos-1],dp[level][pos+1],dp[level-1][pos],fee[level][pos]);
			printf("no res\n");
			break;
		}
	}
	while (!s.empty()){
		printf("%d\n",s.top()+1);
		s.pop();
	}
	return 0;
}

发表评论