zoukankan      html  css  js  c++  java
  • POJ 2353 Ministry

    Ministry
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4220   Accepted: 1348   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
    题目大意:有一栋m层的房子,每层n个房间,如果该房间在第一层或者该房间的上一层的房间或者左右任意一个房间被访问才能访问该房间,问从第一层到最后一层走过的房间的权值和最小的路径。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int fee[105][505];
    int dp[105][505];
    int path[105][505];
    int ans[50005];
    
    int main()
    {
        int m, n;
        int s;
        while(scanf("%d%d", &m, &n) != EOF)
        {
            memset(dp, 0, sizeof(dp));
            memset(path, 0, sizeof(path));
            memset(ans, 0, sizeof(ans));
            for (int i = 1; i <= m; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    scanf("%d", &fee[i][j]);
                }
            }
            for (int i = 1; i <= n; i++)
            {
                dp[1][i] = fee[1][i];
                path[1][i] = i;
            }
            for (int i = 2; i <= m; i++)
            {
                dp[i][1] = dp[i - 1][1] + fee[i][1];
                path[i][1] = 1;
                for (int j = 2; j <= n; j++)
                {
                    if (dp[i - 1][j] < dp[i][j - 1])
                    {
                        dp[i][j] = fee[i][j] + dp[i - 1][j];
                        path[i][j] = j;
                    }
                    else
                    {
                        dp[i][j] = fee[i][j] + dp[i][j- 1];
                        path[i][j] = j - 1;
                    }
                }
                for (int j = n - 1; j > 0; j--)
                {
                    if (dp[i][j + 1] + fee[i][j] < dp[i][j])
                    {
                        dp[i][j] = dp[i][j + 1] + fee[i][j];
                        path[i][j] = j + 1;
                    }
                }
            }
            int temp = 0x7fffffff;
            for (int i = 1; i <= n; i++)
            {
                if (temp > dp[m][i])
                {
                    temp = dp[m][i];
                    s = i;
                }
            }
            int nCount = 0;
            int x = m;
            ans[0] = s;
            while (x != 1)
            {
                nCount++;
                ans[nCount] = path[x][ans[nCount - 1]];
                if (ans[nCount] == ans[nCount - 1])
                {
                    x--;
                }
            }
            for (int i = nCount; i >=0; i--)
            {
                printf("%d
    ", ans[i]);
            }
        }
        return 0;
    }
  • 相关阅读:
    奇 arch/i386/kernel/head.o(.text+0x3e): undefined reference to `stack_start'
    惊爆:当Python代码遇到zip解压炸弹,未做防护的你后悔莫及!
    肝了1个月,做了10个Python可视化动图,有需要的自己拿
    抖音超火的九宫格视频是如何生成的,Python 告诉你答案
    找出文件夹中的相同文件,并移动到指定文件夹中
    8行Python代码绘制疫情地图
    怎么将python项目打包成exe程序?
    利用Pycharm + Django搭建一个简单Python Web项目
    转行Python会经历的几个学习阶段!未来有哪些就业方向?
    一个python脚本就可以B站查找弹幕发送者!
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3233429.html
Copyright © 2011-2022 走看看