zoukankan      html  css  js  c++  java
  • UVA

    Input
    The input consists of a sequence of matrix specifications. Each matrix specification consists of the row
    and column dimensions in that order on a line followed by m · n integers where m is the row dimension
    and n is the column dimension. The integers appear in the input in row major order, i.e., the first n
    integers constitute the first row of the matrix, the second n integers constitute the second row and so
    on. The integers on a line will be separated from other integers by one or more spaces. Note: integers
    are not restricted to being positive.
    There will be one or more matrix specifications in an input file. Input is terminated by end-of-file.
    For each specification the number of rows will be between 1 and 10 inclusive; the number of columns
    will be between 1 and 100 inclusive. No path’s weight will exceed integer values representable using 30
    bits.


    Output
    Two lines should be output for each matrix specification in the input file, the first line represents a
    minimal-weight path, and the second line is the cost of a minimal path. The path consists of a sequence
    of n integers (separated by one or more spaces) representing the rows that constitute the minimal path.
    If there is more than one path of minimal weight the path that is lexicographically smallest should be
    output.
    Note: Lexicographically means the natural order on sequences induced by the order on their elements.
    Sample Input
    5 6
    3 4 1 2 8 6
    6 1 8 2 7 4
    5 9 3 9 9 5
    8 4 1 3 2 6
    3 7 2 8 6 4
    5 6
    3 4 1 2 8 6
    6 1 8 2 7 4
    5 9 3 9 9 5
    8 4 1 3 2 6
    3 7 2 1 2 3
    2 2
    9 10 9 10


    Sample Output
    1 2 3 4 4 5
    16
    1 2 1 5 4 5
    11
    1 1
    19

    首先思考用暴力做,发现大问题和小问题有交叉的关系,所以思考用递归做,进而得出状态转移方程

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    #define N 101
    #define INF 0x7fffffff //最高位不要取f,会变成1哒
    
    
    int main() {
        int A[N][N], d[N][N], p[N][N], n, m, ans, first;
    
        while (cin >> m >> n) {
            ans = INF;
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    cin >> A[i][j];
                }
            }
            for (int j = n - 1; j >= 0; --j) {
                for (int i = 0; i < m; ++i) {
                    if (j == n - 1)
                        d[i][j] = A[i][j];
                    else {
                        int rows[] = {i - 1, i, i + 1};
                        if (i == 0) rows[0] = m - 1;
                        if (i == m - 1) rows[2] = 0;
                        sort(rows, rows + 3); //利用排序来取索引和最小值
                        int v = INF;
                        for (int k = 0; k < 3; ++k) {
                            if (v > d[rows[k]][j + 1]) {
                                v = d[rows[k]][j + 1];
                                p[i][j] = rows[k]; //记录下一步的路径
                            }
                        }
                        d[i][j] = v + A[i][j];
                    }
                    if (j == 0 and ans > d[i][j]) {//由上往下扫,形成字典序
                        ans = d[i][j], first = i;
                    }
                }
            }
            cout << first + 1;
            for (int j = 0, r = first; j < n - 1; ++j) {//路径的输出
                r = p[r][j];
                printf(" %d", r + 1);
            }
            printf("
    %d
    ", ans);
        }
    }
  • 相关阅读:
    异步底层代码实现邮件发送
    MongoDB+Echarts+DWebSocket
    celery定时任务+redis有序集合实现实时访问人数
    位运算+数据库两种方式实现中间件权限操作
    cocoapod 引入url
    pdf转xml
    Flutter项目安卓下载地址
    ios Mac 利用SVN进行cocoapod私有库的使用
    KVO
    类别和类扩展的区别
  • 原文地址:https://www.cnblogs.com/wangsong/p/8016998.html
Copyright © 2011-2022 走看看