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 ...
随机推荐
- python系列之(4)豆瓣图书《平凡的世界》书评及情感分析
本篇主要是通过对豆瓣图书<平凡的世界>短评进行抓取并进行分析,并用snowNLP对其进行情感分析. 用到的模块有snowNLP,是一个python库,用来进行情感分析. 1.抓取数据 我们 ...
- Java练习 SDUT-2053_整理音乐
整理音乐 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请用链表完成下面题目要求. xiaobai 很喜欢音乐,几年来 ...
- TIJ——Chapter Thirteen:Strings
String 对象是不可修改的,对于被String 重载的'+' 和'+=' 运算符来说,当你用它们来连接两个String 对象的时候,它在底层并不会对于每一次连接均生成一个String 对象,取而代 ...
- MaxCompute 费用暴涨之新增SQL分区裁剪失败
现象:因业务需求新增了SQL任务,这SQL扫描的表为分区表,且SQL条件里表只指定了一个分区,按指定的分区来看数据量并不大,但是SQL的费用非常高.费用比预想的结果相差几倍甚至10倍以上. 若只知道总 ...
- Android 系统字体和颜色样式
Android 字体和颜色 对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有时需要控制字体的大小 ...
- 3DMAX安装失败怎样卸载重新安装3DMAX,解决3DMAX安装失败的方法总结
技术帖:3DMAX没有按照正确方式卸载,导致3DMAX安装失败.楼主也查过网上关于如何解决3DMAX安装失败的一些文章,是说删除几个3DMAX文件和3DMAX软件注册表就可以解决3DMAX安装失败的问 ...
- sn图书spider
# -*- coding: utf-8 -*-import scrapyfrom copy import deepcopy class SnbookSpider(scrapy.Spider): nam ...
- List<object> 转 List<T>
List<TAXIWAY_CENTER_LINE> kk = allObjs.Where(c => c.ToString() == "AMXM.TAXIWAY_CENTER ...
- angular.extend vs angular.copy
1.angular.copy angular.copy(source, [destination]); // source: copy的对象. 可以使任意类型, 包括null和undefined. ...
- python 多线程,tthread模块比较底层,而threading模块是对thread做了一些包装,multithreading
Python多线程详解 2016/05/10 · 基础知识 · 1 评论· 多线程 分享到:20 本文作者: 伯乐在线 - 王海波 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 1.多线程的 ...