zoukankan      html  css  js  c++  java
  • codeforces 677D(分层图dp)

    Codeforces 677D

    传送门:https://codeforces.com/contest/677/problem/D

    题意:

    给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐标(1,1)开始走,要求你从权值为1的点,走到权值为2的点,依次类推,最终走到权值为p的点的最短路径是多少

    题解:

    分层图dp

    [dp[i][j]表示到达点(i,j)所需要的最短距离是多少\ dis维护一个纵列上的距离\ vis维护一个当前走到的位置\ 转移:dp[r][c] = min(dp[r][c], dis[t][c] + abs(r - t) hspace{1cm} tin[1,n]&&vis[r][c]=i\ dis[r][t] = min(dis[r][t], dp[r][c] + abs(c - t));hspace{1cm} tin[1,m]&&vis[r][t]=i+1\ ]

    代码:

    #include <set>
    #include <map>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <ctime>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef long long ll;
    typedef pair<LL, LL> pLL;
    typedef pair<LL, int> pLi;
    typedef pair<int, LL> pil;;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define bug printf("*********
    ")
    #define FIN freopen("input.txt","r",stdin);
    #define FON freopen("output.txt","w+",stdout);
    #define IO ios::sync_with_stdio(false),cin.tie(0)
    #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
    "
    #define debug2(x, y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x, y, z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    
    const double eps = 1e-8;
    const int mod = 1e9 + 7;
    const int maxn = 3e5 + 5;
    const int INF = 0x3f3f3f3f;
    const LL INFLL = 0x3f3f3f3f3f3f3f3f;
    struct EDGE {
        int v, nxt;
    } edge[maxn << 1];
    int head[maxn], tot;
    
    void add_edge(int u, int v) {
        edge[tot].v = v, edge[tot].nxt = head[u], head[u] = tot++;
    }
    
    int mp[500][505];
    int n, m, k, dp[305][305], vis[305][305], dis[305][305];
    vector<pii> b[300 * 300 + 10];
    
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        int n, m, p;
        scanf("%d%d%d", &n, &m, &p);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &mp[i][j]);
                b[mp[i][j]].push_back(make_pair(i, j));
            }
        }
        b[0].push_back(make_pair(1, 1));
        memset(dp, 63, sizeof(dp));
        for (int i = 0; i <= p; i++) {
            int len = b[i].size();
            for (int j = 0; j < len; j++) {
                int r = b[i][j].first;
                int c = b[i][j].second;
                if (r == 1 && c == 1 && dp[r][c] == 0) {
                    dp[r][c]=INF;
                }
                for (int t = 1; t <= n; t++) {
                    if (vis[t][c] == i) {
                        dp[r][c] = min(dp[r][c], dis[t][c] + abs(r - t));
                    }
                }
            }
            for (int j = 0; j < len; j++) {
                int r = b[i][j].first;
                int c = b[i][j].second;
                for (int t = 1; t <= m; t++) {
                    if (vis[r][t] != i + 1) {
                        vis[r][t] = i + 1;
                        dis[r][t] = dp[r][c] + abs(c - t);
                    } else {
                        dis[r][t] = min(dis[r][t], dp[r][c] + abs(c - t));
                    }
                }
            }
        }
        printf("%d
    ", dp[b[p][0].first][b[p][0].second]);
    
        return 0;
    }
    
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    mount挂载命令
    centos和redhat的区别
    查不认识字的方法
    Prometheus介绍及docker安装方式
    Pinpoint介绍及docker安装方式
    虚拟机安装linux
    yum命令
    docker命令
    PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)
    PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11161015.html
Copyright © 2011-2022 走看看