天天看點

URAL 1029 Ministry(記錄路徑的dp)

題目很好了解就是數字三角形的變形版本,每個房間可以由它的上面和他的左邊還有右邊過來。求出一個最小的值,然後列印出路徑。

列印路徑時出現了一些小問題,應該預輸出最多一個路徑,否則會出錯。

PS:出問題時問虎哥,虎哥說别人正确的做法很重要,但是你自己錯在哪裡更重要。終于d出來了啊。

1029. Ministry

Time limit: 1.0 second

Memory limit: 64 MB

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 output
3 4
10 10 1 10
2 2 2 10
1 10 10 10
      
3 3 2 1 1      
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7fffffff
#define PI 3.1415926535898


const int maxn = 510;

using namespace std;

LL dp[maxn][maxn];
LL num[maxn][maxn];
LL f[maxn*maxn];

struct node
{
    long x, y;
} pre[maxn][maxn];


int main()
{
    int n, m;
    scanf("%d %d",&n, &m);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            scanf("%lld",&num[i][j]);
    for(int i = 0; i <= m; i++)
    {
        pre[1][i].x = 0;
        pre[1][i].y = 0;
    }
    for(int i = 1; i <= m; i++)
        dp[1][i] = num[1][i];
    int i, j;
    for(i = 2; i <= n; i++)
    {
        for(j = 1; j <= m; j++)
        {
            dp[i][j] = dp[i-1][j]+num[i][j];
            pre[i][j].x = i-1;
            pre[i][j].y = j;
        }
        for(j = 2; j <= m; j++)
        {
            if(dp[i][j] > dp[i][j-1]+num[i][j])
            {
                pre[i][j].x = i;
                pre[i][j].y = j-1;
                dp[i][j] = dp[i][j-1]+num[i][j];

            }
        }
        for(j = m-1; j >= 1; j--)
        {
            if(dp[i][j] > dp[i][j+1]+num[i][j])
            {
                pre[i][j].x = i;
                pre[i][j].y = j+1;
                dp[i][j] = dp[i][j+1]+num[i][j];
            }
        }
    }
    LL Min = INF;
    int t;
    for(int i = 1; i <= m; i++)
    {
        if(dp[n][i] < Min)
        {
            Min = dp[n][i];
            t = i;
        }
    }
    int x, y;
    int s = 1;
    f[0] = t;
    int xx;
    int yy;
    x = n;
    y = t;
    while(1)
    {
        if(x == 0 || y == 0)
            break;
        if(pre[x][y].y > 0)
            f[s++] = pre[x][y].y;
        xx = x;
        yy = y;
        x = pre[xx][yy].x;
        y = pre[xx][yy].y;
    }
    for(int i = s-1; i >= 1; i --)
        cout<<f[i]<<" ";
    cout<<f[0]<<endl;;
    return 0;
}