poj 2353 类似数塔(需标记路径)

http://poj.org/problem?id=2353

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.
大意:每个点(i,j)可以从(i,j+1)(i,j-1)(i-1,j)转移而来,求从第一行到最后一行使权值最小的路径

/**
定义pre[i][j]表示i,j点是从哪一个点转移来的。为j+1表示从(i,j+1)转移,为j-1 -->(i,j-1),为j-->(i-1,j);
分别从前往后和从后往前分别转移:
先是从左到右:dp [ i ][ j ]=Min(dp[ i-1 ][ j ],dp[ i ][ j-1])+w;
再是从右到左:dp [ i ][ j ]=Min(dp[ i ][ j ],dp[ i ][ j+1] )+ w ;
*/

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;

const int N=555;
int n,m;
int pre[N][N],a[N][N],dp[N][N];

void print(int i,int j)
{
    if(i==1)
    {
        printf("%d\n",j);
        return;
    }
    if(pre[i][j]==j)
        print(i-1,j);
    else
        print(i,pre[i][j]);
    printf("%d\n",j);
}
int main()
{
    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];
        for(int i=1; i<=n; i++)
            dp[i][0]=dp[i][m+1]=0x3f3f3f3f3f;
        for(int i=2; i<=n; i++)
        {
            //从前往后遍历
            for(int j=1; j<=m; j++)
            {
                if(dp[i][j-1]>dp[i-1][j])
                {
                    dp[i][j]=dp[i-1][j]+a[i][j];
                    pre[i][j]=j;
                }
                else
                {
                    dp[i][j]=dp[i][j-1]+a[i][j];
                    pre[i][j]=j-1;
                }
            }
            //从后往前遍历
            for(int j=m; j>0; j--)
            {
                if(dp[i][j]>dp[i][j+1]+a[i][j])
                {
                    dp[i][j]=dp[i][j+1]+a[i][j];
                    pre[i][j]=j+1;
                }
            }
        }
        int t;
        int ans=0x3f3f3f3f3f;
        for(int i=1; i<=m; i++)
        {
            if(ans>dp[n][i])
            {
                ans=dp[n][i];
                t=i;
            }
        }
        print(n,t);
    }
    return 0;
}

/**

4 4
9 9 1 1
9 1 1 9
9 1 9 9
9 1 1 9

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值