前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-2353 Ministry(动态规划)

POJ-2353 Ministry(动态规划)

作者头像
ShenduCC
发布2018-04-26 11:08:54
5410
发布2018-04-26 11:08:54
举报
文章被收录于专栏:算法修养算法修养

Ministry Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4761 Accepted: 1528 Special Judge 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时,要分从左到右和从右到左两种情况

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int a[105][505];
int dp[105][505];
int b[105][505];
int n,m;
int res[50005];
int main()
{
    int ans;
    int MAX=pow(10.0,9.0)*2;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(res,0,sizeof(res));
        memset(b,0,sizeof(b));
        ans=MAX;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
                dp[i][j]=MAX;
                b[i][j]=j;
            }
        }
        for(int i=1;i<=m;i++)
            dp[1][i]=a[1][i];
        for(int i=2;i<n;i++)
        {
            int term;
            dp[i][1]=dp[i-1][1]+a[i][1];
            for(int j=2;j<=m;j++)
            {
                if(dp[i-1][j]>dp[i][j-1])
                    b[i][j]=j-1;
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])+a[i][j];
            }
            term=dp[i][m];
            dp[i][m]=dp[i-1][m]+a[i][m];
            for(int j=m-1;j>=1;j--)
            {
                if(dp[i][j+1]+a[i][j]<dp[i][j])
                    b[i][j]=j+1;
                dp[i][j]=min(dp[i][j],dp[i][j+1]+a[i][j]);
            }
            dp[i][m]=min(dp[i][m],term);
            if(term<dp[i][m])
                b[i][m]=m-1;
        }
        int num=0;
        for(int i=1;i<=m;i++)
        {
            if(ans>dp[n-1][i]+a[n][i])
            {
                ans=dp[n-1][i]+a[n][i];
                num=i;
            }
        }
        res[0]=num;
        int tag=n;
        int cot=0;
        while(tag>1)
        {
            res[cot+1]=b[tag][res[cot]];
            if(b[tag][res[cot]]==res[cot])
                tag--;
            cot++;
        }
        for(int i=cot;i>=0;i--)
            printf("%d\n",res[i]);

    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-01-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档