• 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
  • 相关阅读:
    试玩mpvue,用vue的开发模式开发微信小程序
    laravel 整合 swoole ,并简单 ab 测试对比性能以及在 PHPstorm 中利用debug调试配置swoole服务中的PHP代码
    移动端固定头部和固定左边第一列的实现方案(Vue中实现demo)
    PhpStorm 2017.3 版本在 Mac 系统 macOS High Sierra 版本 10.13.3 中运行很卡顿
    xdebug : Debug session was finished without being paused
    SVN checkout 出的项目在PHPstorm中打开没有subversion(SVN)选项按钮怎么办?
    PHP应用的CI/CD流程实践与学习:一、PHP运行环境的准备
    Mac环境下PHPstorm配置xdebug开发调试web程序
    『备忘录』elasticsearch 去重分页查询
    Mac下docker搭建lnmp环境 + redis + elasticsearch
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11161015.html
Copyright © 2020-2023  润新知