codeforces 677D(分层图dp)
Codeforces 677D
传送门:https://codeforces.com/contest/677/problem/D
题意:
给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐标(1,1)开始走,要求你从权值为1的点,走到权值为2的点,依次类推,最终走到权值为p的点的最短路径是多少
题解:
分层图dp
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)的更多相关文章
- 【bfs分层图 dp】hihocoder#1147 : 时空阵
最短路径树上分层dp的一类套路吧 题目大意 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和 ...
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP
BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...
- [luogu1073 Noip2009] 最优贸易 (dp || SPFA+分层图)
传送门 Description C 国有n 个大城市和m 条道路,每条道路连接这n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这m 条道路中有一部分为单向通行的道路,一部分 为 ...
- 线性dp,分层图思想
题目大意:给你一串数字,一串运算符,求递推用完运算符时答案的最大值----->线性dp dp[i][j] i表示所用数字的个数 j表示所用字符的个数 分层图思想 所有字符必须用完 所以取最后 ...
- Codeforces Gym Joyride(分层图,dijkstra)
题意:有一张图,每条边有一个边权t表示经过所花时间,每个点有两个权t.p,分别表示经过该点所花时间和所花费用,要求找一条路径,从点1出发再回到点1,所花时间恰好为x且费用最小,输出其费用,找不到则输出 ...
- 一本通 高手训练 1782 分层图 状压dp
LINK:分层图 很精辟的一道题 写的时候没带脑子 导致搞了半天不知道哪错了. 可以想到状压每次到某一层的状态 然后这个表示方案数 多开一维表示此时路径条数的奇偶即可. 不过显然我们只需要知道路径条数 ...
- POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?
然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...
- poj3635Full Tank?[分层图最短路]
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7248 Accepted: 2338 Descri ...
随机推荐
- MVVM框架搭建
以下是概要的目录结构,其中View,ViewModel,Model正代表的是MVVM的标识. View:页面window或者UserControl Model:数据模型对象 ViewModel:与Vi ...
- 从 FastAdmin 看国内开源现状
从 FastAdmin 看国内开源现状 在做 FastAdmin 之前看过太多的国内开源软件,很多没有建立起正反馈,最终而烂尾. 国内开源者有时候收到的是负反馈. 比如遇到问题埋怨开源作者,对开源作者 ...
- Java练习 SDUT-3338_计算各种图形的周长(接口与多态)
计算各种图形的周长(接口与多态) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 定义接口Shape,定义求周长的方法l ...
- linux cat /etc/passwd 说明
通常在Linux系统中,用户的关键信息被存放在系统的/etc/passwd文件中,系统的每一个合法用户账号对应于该文件中的一行记录.这行记录定义了每个用户账号的属性.下面是一个passwd文件的示例( ...
- Arthas用法
简介 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 这个类从哪个 jar 包加载的? 为什么会报各种类相关的 ...
- 冒泡排序 Day07
package com.sxt.arraytest2; /* * 冒泡排序 * 思想:两两交换 一路大的向右 */ import java.util.Arrays; public class Bubb ...
- Class.forName(“com.mysql.jdbc.Driver”)
传统的使用jdbc来访问数据库的流程为: Class.forName(“com.mysql.jdbc.Driver”); String url = “jdbc:mysql://localhost:33 ...
- 解释型语言Python
- 01docker简单使用和配置(容器、镜像)
一:容器中运行简单应用程序 1:hello world 使用docker可以在容器中运行应用程序,使用docker run命令即可.比如下面的命令: $ docker run ubuntu /bin ...
- Python基础:11变量作用域和闭包
一:变量作用域 变量可以是局部域或者全局域.定义在函数内的变量有局部作用域,在一个模块中最高级别的变量有全局作用域. 全局变量的一个特征是除非被删除掉,否则它们的存活到脚本运行结束,且对于所有的函数, ...