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} t\in[1,n]\&\&vis[r][c]=i\\
dis[r][t] = min(dis[r][t], dp[r][c] + abs(c - t));\hspace{1cm} t\in[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("*********\n")
#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)<<"]\n"
#define debug2(x, y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x, y, z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n" 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\n", dp[b[p][0].first][b[p][0].second]); return 0;
}

codeforces 677D(分层图dp)的更多相关文章

  1. 【bfs分层图 dp】hihocoder#1147 : 时空阵

    最短路径树上分层dp的一类套路吧 题目大意 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和 ...

  2. POJ 3635 Full Tank? 【分层图/最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP

    BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...

  4. [luogu1073 Noip2009] 最优贸易 (dp || SPFA+分层图)

    传送门 Description C 国有n 个大城市和m 条道路,每条道路连接这n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这m 条道路中有一部分为单向通行的道路,一部分 为 ...

  5. 线性dp,分层图思想

    题目大意:给你一串数字,一串运算符,求递推用完运算符时答案的最大值----->线性dp dp[i][j] i表示所用数字的个数   j表示所用字符的个数 分层图思想 所有字符必须用完 所以取最后 ...

  6. Codeforces Gym Joyride(分层图,dijkstra)

    题意:有一张图,每条边有一个边权t表示经过所花时间,每个点有两个权t.p,分别表示经过该点所花时间和所花费用,要求找一条路径,从点1出发再回到点1,所花时间恰好为x且费用最小,输出其费用,找不到则输出 ...

  7. 一本通 高手训练 1782 分层图 状压dp

    LINK:分层图 很精辟的一道题 写的时候没带脑子 导致搞了半天不知道哪错了. 可以想到状压每次到某一层的状态 然后这个表示方案数 多开一维表示此时路径条数的奇偶即可. 不过显然我们只需要知道路径条数 ...

  8. POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?

    然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...

  9. poj3635Full Tank?[分层图最短路]

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7248   Accepted: 2338 Descri ...

随机推荐

  1. js不停地触发按钮的事件

    例子:不断地发匿名邮件 http://tool.chacuo.net/mailsend 在控制台写:setInterval('$(".convert :input[arg]").t ...

  2. 用python爬虫抓站的一些技巧总结 zz

    用python爬虫抓站的一些技巧总结 zz 学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本, ...

  3. nginx 做反向代理

    1.Nginx的常用配置大家可以去搜一下,有很多优秀的博客,我这篇文章要实现的需求是: a.根据访问的域名不同,跳转到不同的项目(html首页,80端口) b.拦截访问中所有带有api的请求,转发到后 ...

  4. 源码篇:MBProgressHUD

    源码来源:点此 版本:0.9.1 MBProgressHUD是一个显示HUD窗口的第三方类库,用于在执行一些后台任务时,在程序中显示一个表示进度的loading视图和两个可选的文本提示的HUD窗口.我 ...

  5. python HTTP请求过程的两种常见异常

  6. ps学习教程

    1.通道抠图:http://www.16xx8.com/photoshop/tongdaokoutu/

  7. ELK2之ELK的语法学习

    1.回顾 (1)es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,提供简单的RESTful API来隐藏Lucene的复杂性. es除了全文搜索引擎之外,还可以这样描述它 ...

  8. laravel 自定义常量方法

    laravel 自定义常量方法 版本5以上验证OK 常量定义方案A: step1 按路径生成constants文件app/config/constants.php step2 constants文件里 ...

  9. 一维数组的求平均成绩 Day06

    package com.sxt.arraytest1; /* * 求班里学生的平均成绩,以及成绩的综合 输出每个同学的成绩 */ import java.util.Arrays; import jav ...

  10. python 清空文件夹

    #!/usr/bin/env python# -*- coding:utf-8 -*-import os def del_file(path): for i in os.listdir(path): ...