Ministry

传送门: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:
the official works on the 1st floor;
the document is signed by the official working in the room with the same number but situated one floor below;
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 109.
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 **
3 4
10 10 1 10
2 2 2 10
1 10 10 10
3 3 2 1 1‘
output
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 109.

题意:意思就是说,给你n*m的一个矩阵,然后每个位置有一个权值mp[i][j],问你从第一行走到最后一行的最小路径权值和(一个位置可以往下,左,右走),然后输出你每次走的纵坐标即可。
思路:用dp数组记录一下走到当前的结点的最小权值,因为可以左右走,那么我们可以先对dp[i][j]初始化为就是简单的从上直走下来的,然后我们左边更新走一下(因为更新是用左边,所以我们要保证左边当前已经最优,那么我们就用从左往右for一遍,更新dp数组),同样的道理右边也来一次,同一行左右更新不能分开来计算,那样当行数多的时候,左右不能同时最优,就会影响后面dp的正确性。然后我们记录一下dp路径,(从哪个点更新,就记录为当前点的父点)然后反向输出即可,或者把图反存,然后直接输出(我是用这种)。
AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL dp[105][505];
LL mp[105][505];
LL lu[105][505][2];
LL ans[50005];
int main()
{
    LL n,m;
    scanf("%lld %lld",&n,&m);
    for(LL i=0;i<n;i++)
    {
        for(LL j=1;j<=m;j++)
        {
            scanf("%lld",&mp[n-i][j]);
        }
    }
    memset(dp,0x3f3f3f3f,sizeof(dp));
    for(LL j=1;j<=m;j++)
    {
        dp[1][j]=mp[1][j];
    }
    for(LL i=2;i<=n;i++)
    {
        for(LL j=1;j<=m;j++)
        {
            lu[i][j][0]=i-1;
            lu[i][j][1]=j;
            dp[i][j]=dp[i-1][j]+mp[i][j];
            if(j>1&&(dp[i][j]>(dp[i][j-1]+mp[i][j])))
            {
                dp[i][j]=dp[i][j-1]+mp[i][j];
                lu[i][j][0]=i;
                lu[i][j][1]=j-1;
            }
        }
        for(LL j=m-1;j>=1;j--)
        {
            if((dp[i][j]>dp[i][j+1]+mp[i][j]))
            {
                dp[i][j]=dp[i][j+1]+mp[i][j];
                lu[i][j][0]=i;
                lu[i][j][1]=j+1;
            }
            if(dp[i][j]>dp[i-1][j]+mp[i][j])
            {
                dp[i][j]=dp[i-1][j]+mp[i][j];
                lu[i][j][0]=i-1;
                lu[i][j][1]=j;
            }
        }
    }
    //printf("%lld %lld**\n",lu[2][2][0],lu[2][2][1]);
//    for(LL i=2;i<=n;i++)
//    {
//        for(LL j=m-1;j>=1;j--)
//        {
//            if((dp[i][j]>dp[i][j+1]+mp[i][j]))
//            {
//                dp[i][j]=dp[i][j+1]+mp[i][j];
//                lu[i][j][0]=i;
//                lu[i][j][1]=j+1;
//            }
//            if(dp[i][j]>dp[i-1][j]+mp[i][j])
//            {
//                dp[i][j]=dp[i-1][j]+mp[i][j];
//                lu[i][j][0]=i-1;
//                lu[i][j][1]=j;
//            }
//        }
//    }
    //printf("%lld %lld**\n",lu[2][3][0],lu[2][3][1]);
    LL x,y;
    LL minn=1e15;
    for(LL i=1;i<=m;i++)
    {
        if(dp[n][i]<=minn)
        {
            minn=dp[n][i];
            x=n;y=i;
        }
    }
//    for(LL i=1;i<=n;i++)
//    {
//        for(LL j=1;j<=m;j++)
//        {
//            printf("%lld ",dp[i][j]);
//        }
//        printf("\n");
//    }
//printf("%lld %lld\n",x,y);
    LL u,w;
    while(1)
    {
        u=x,w=y;
        if(y==0)
        {
            break;
        }
        printf("%lld\n",y);
        x=lu[u][w][0];
        y=lu[u][w][1];
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43402296/article/details/105348053