• 【动态规划】poj2353Ministry


    拓扑序……好些玄妙

    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做两次……

    只不过想记录一下这个dp拓扑序的问题。

    对于点$(x,y)$需要先从上面转移,再从两边转移。虽然看上去随便怎么样好像都一样、会根据最优解覆盖,但是实际上是要考虑这个dp的拓扑序的……

     =

     1 #pragma GCC optimize(2)
     2 #include<cstring>
     3 #include<cctype>
     4 #include<cstdio>
     5 const int maxn = 503;
     6 
     7 int f[maxn][maxn],a[maxn][maxn];
     8 int n,m,g[maxn][maxn],cnt;
     9 
    10 int read()
    11 {
    12     char ch = getchar();
    13     int num = 0;
    14     bool fl = 0;
    15     for (; !isdigit(ch); ch = getchar())
    16         if (ch=='-') fl = 1;
    17     for (; isdigit(ch); ch = getchar())
    18         num = (num<<1)+(num<<3)+ch-48;
    19     if (fl) num = -num;
    20     return num;
    21 }
    22 void dfs(int layer, int x)
    23 {
    24     if (layer!=1&&!g[layer][x]) dfs(layer-1, x);
    25     else if (g[layer][x]) dfs(layer, x+g[layer][x]);
    26     printf("%d
    ",x);
    27 }
    28 int main()
    29 {
    30     register int i,j,tt = 0;
    31     n = read(), m = read();
    32     for (i=1; i<=n; i++)
    33         for (j=1; j<=m; j++)
    34             a[i][j] = read(), f[i][j] = 2e9;
    35     f[n][0] = 2e9;
    36     for (i=1; i<=m; i++)
    37         f[1][i] = a[1][i];
    38     for (i=2; i<=n; i++)
    39     {
    40         for (j=1; j<=m; j++)
    41         {
    42             if (f[i][j] > f[i-1][j]+a[i][j]){
    43                 f[i][j] = f[i-1][j]+a[i][j];
    44                 g[i][j] = 0;
    45             }
    46             if (j!=1&&f[i][j] > f[i][j-1]+a[i][j]){
    47                 f[i][j] = f[i][j-1]+a[i][j];
    48                 g[i][j] = -1;
    49             }
    50         }
    51         for (j=m-1; j>=1; j--)
    52         {
    53             if (f[i][j] > f[i][j+1]+a[i][j]){
    54                 f[i][j] = f[i][j+1]+a[i][j];
    55                 g[i][j] = 1;
    56             }
    57         }
    58     }
    59     for (i=1; i<=m; i++)
    60         if (f[n][tt] > f[n][i]) tt = i;
    61     dfs(n, tt);
    62     return 0;
    63 }

    END

  • 相关阅读:
    HTML的基本骨架
    2017.7.27
    2017.7.26
    2017.7.25
    2017.7.24
    2017.7.22
    2017.7.21
    Javascript Step by Step
    Javascript Step by Step
    Javascript Step by Step
  • 原文地址:https://www.cnblogs.com/antiquality/p/9285350.html
Copyright © 2020-2023  润新知