1 solutions

  • 0
    @ 2026-11-13 19:57:16

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 2005;
    int a[MAXN][MAXN];
    long long dp[MAXN][MAXN];
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int N, M;
        cin >> N >> M;
    
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                cin >> a[i][j];
    
        // 第一列初始化
        for (int i = 0; i < N; i++)
            dp[i][0] = a[i][0];
    
        // 从第二列开始递推
        for (int j = 1; j < M; j++) {
            for (int i = 0; i < N; i++) {
                long long from_same = dp[i][j - 1];
                long long from_next = dp[(i - 1 + N) % N][j - 1];  // 下一行
                dp[i][j] = a[i][j] + min(from_same, from_next);
            }
        }
    
        // 答案:最后一列的最小值
        long long ans = LLONG_MAX;
        for (int i = 0; i < N; i++)
            ans = min(ans, dp[i][M - 1]);
    
        cout << ans << endl;
        return 0;
    }
    
    • 1

    Information

    ID
    103
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By