URAL1029——DP+回溯——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:
  1. the official works on the 1st floor;
  2. the document is signed by the official working in the room with the same number but situated one floor below;
  3. 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 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 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

inputoutput
3 4
10 10 1 10
2 2 2 10
1 10 10 10
3 3 2 1 1

Notes

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.
大意:N跟M。。。好坑 输入n,m表示这个大楼的层数以及房间数,从最上面的一行中的一个开始到最下面一行中的一个,把文件签下来,只要求输出列数,不要求行数
状态转移方程  dp[i][j] = min(dp[i-1][j]+a[i][j],dp[i][j-1]+a[i][j],dp[i][j+1]+a[i][j])
学到了存储状态的方法开一个p,对于不同的状态用不同的来保存,最后只要找到最下面一个中的最大值,对这个状态进行dfs,寻找原来保存的状态,进行输出
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110][550];
long long  dp[110][550];
int p[110][550];
void dfs(int x,int y)
{
    if(p[x][y] == -1){
        printf("%d",y);
        return ;
    }
    else if(p[x][y] == 0){
        dfs(x-1,y);
    }
    else if(p[x][y] == 1){
        dfs(x,y-1);
    }
    else dfs(x,y+1);
    printf(" %d",y);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i = 1; i <= n ; i++)
            for(int j = 1; j <= m ;j++)
                scanf("%d",&a[i][j]);
        for(int i = 1; i <= m ; i++){
            dp[1][i] = a[1][i];
            p[1][i] = -1;
        }
        for(int i = 2; i <= n ;i++){
            for(int j = 1; j <= m ;j++){
                dp[i][j] = dp[i-1][j] + a[i][j];
                p[i][j] = 0;
            }
            for(int j = 2; j <= m ; j++){
                if(dp[i][j-1] + a[i][j] < dp[i][j]){
                    dp[i][j] = dp[i][j-1] + a[i][j];
                    p[i][j] = 1;
                }
            }
            for(int j = m-1; j >= 1; j--){
                if(dp[i][j+1] + a[i][j] < dp[i][j]){
                    dp[i][j] = dp[i][j+1] + a[i][j];
                    p[i][j] = 2;
                }
            }
        }
        int x = n,y = 1;
        int max1 = dp[n][1];
            for(int i = 2; i <= m;i++){
                if(max1 > dp[n][i]){
                    y = i;
                    max1 = dp[n][i];
                }
            }
            dfs(x,y);
            printf("
");
        }
        return 0;
}
        

  

 
原文地址:https://www.cnblogs.com/zero-begin/p/4497757.html