cqyz oj | [HB] Training Problems accreditation P1419 | DP Dynamic Programming

Description

HB want to do a visa, a permit is that building a layer of M, N offices on each floor, numbered 1..N, each office has a staff visas, visas need to get a visa to a member of the M layer seal to be effective. Each member must meet one of the following visa three conditions will give HB Stamp:

  1. The visa staff on the first floor.
  2. HB visa has given this positive downstairs visa members (same room number) visa overshadowed member of the chapter.
  3. HB visa has given adjacent rooms this visa members (a difference of room number 1, on the same floor) visa overshadowed member of the chapter.

Each member of the visa stamp will charge a fee, this fee is not more than 1 billion. Find the minimum cost of stamp line, the entry into force of the visa.

Input

第1行两个整数 M 和 N 。  接下来M行每行 N 个整数,第i行第j个数表示第i层的第j个签证员收取的费用。

Output

  The minimum cost of output.

Sample Input 1

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

Sample Output 1

8

Hint

1<=M<=100,1<=N<=500

At first glance very simple, just set the state of the function F (i, j) represents the minimum cost to the i-th layer of the j-th rooms needed, transfer equation:
\ (F (i, j) = min (F (i. 1- , j), f (i,
j-1), f (i, j + 1)) + a [i] [j] \) takes where a [i] [j] for the current room.
Initialization
\ (F (I, J) = INF \)
\ (F (. 1, J) = A [. 1] [J] |. 1 <= J <= m \)

But if each layer so to transfer, either from left to right or from right to left to calculate calculations have after-effect , because if the update right from left, so used to calculate f (i, j) f ( i, j + 1) has not been updated, it will cause an error

The solution is left for each layer start right away update f (i, j) with f (i, j-1), which finish layer down then right to left with f (i, j + 1) Update f (i, j), to ensure that the number is not used in the calculation of the unknown.

\ (Ans = min (f (
n, j) | 1 <= j <= m) \) I code is to go down from the current, a difference

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int a[105][505];
int f[105][505];
void init(){
    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=0;i<=n+1;i++)
        for(int j=0;j<=m+1;j++)
            f[i][j]=inf;
}
void dp(){
    for(int i=1;i<=m;i++)f[n][i]=a[n][i];
    
    for(int i=n-1;i>=1;i--){
        for(int j=m;j>=1;j--)
            f[i][j]=min(f[i+1][j],f[i][j+1])+a[i][j];//合并右走和下走 
        for(int j=1;j<=m;j++)//分开循环防后效 
            f[i][j]=min(f[i][j],f[i][j-1]+a[i][j]);//左走 
    }
    
    int ans=inf;
    for(int i=1;i<=m;i++)
        ans=min(ans,f[1][i]);
    printf("%d",ans);
}
int main(){
    init();
    dp();
    return 0;
}

Guess you like

Origin www.cnblogs.com/de-compass/p/11234809.html