Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块
题目链接:
http://codeforces.com/contest/677/problem/D
题意:
让你求最短的从start->...->1->...->2->...->3->...->...->p的最短路径。
题解:
这题dp的阶段性还是很明显的,相同的值得方格为同一个阶段,然后求从阶段1->2->3...->p的阶段图最短路。
初始化所有a[x][y]==1的格子为起始点到(x,y)坐标的距离。
方程式为dp[x1][y1]=min(dp[x1][y1],dp[x2][y2]+abs(x2-x1)+abs(y2-y1)),其中a[x1][y1]=a[x2][y2]+1。
但是这要n*n*m*m的复杂度。
x阶段到x+1阶段的转移数为cnt[x]*cnt[x+1],这个太大,并且多来几个就会爆了,但注意如果cnt[x]*cnt[x+1]大了,其他的如
cnt[x']*cnt[x'+1]就有可能会变小,因为sum(cnt[i])是固定的,
那么根据这个特性,我们可以考虑分块!,当cnt[x]*cnt[x+1]<=m*n的时候直接暴力,如果大了就用最短路(spfa就可以)
这样跑出来的时间复杂度为O(n*m*sqrt(n*m)*log(n*m)),那个log(n*m)是spfa跑出来的。
具体的均摊时间复杂度证明:http://codeforces.com/blog/entry/45181?#comment-297475
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<utility>
#include<vector>
#include<algorithm>
#include<queue>
#define mp make_pair
#define X first
#define Y second
using namespace std; const int maxn = ; int dp[maxn][maxn],mat[maxn][maxn];
int cnt[maxn*maxn],inq[maxn][maxn],d[maxn][maxn];
vector<pair<int,int> > G[maxn*maxn];
int n, m, p; inline int get_dis(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
} const int dx[] = { -,,, };
const int dy[] = { ,,-, }; void init() {
memset(dp, 0x7f, sizeof(dp));
memset(cnt, , sizeof(cnt));
} int main() {
while (scanf("%d%d%d", &n, &m, &p) == && n) {
init();
int xt, yt;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
scanf("%d", &mat[i][j]);
if (mat[i][j] == ) dp[i][j] = get_dis(,,i,j);
if (mat[i][j] == p) xt = i, yt = j;
cnt[mat[i][j]]++;
G[mat[i][j]].push_back(mp(i, j));
}
}
for (int i = ; i <= p; i++) {
if (cnt[i - ] * cnt[i] <= m*n) {
for (int j = ; j < G[i].size(); j++) {
int x2 = G[i][j].X, y2 = G[i][j].Y;
for (int k = ; k < G[i - ].size(); k++) {
int x1 = G[i - ][k].X, y1 = G[i - ][k].Y;
dp[x2][y2] = min(dp[x2][y2], dp[x1][y1] + get_dis(x1, y1, x2, y2));
}
}
}
else {
memset(d, 0x7f, sizeof(d));
memset(inq, , sizeof(inq));
queue<pair<int, int> > Q;
for (int j = ; j < G[i - ].size(); j++) {
pair<int, int> u = G[i - ][j];
d[u.X][u.Y] = dp[u.X][u.Y], inq[u.X][u.Y] = ;
Q.push(mp(u.X, u.Y));
}
while (!Q.empty()) {
pair<int, int> u = Q.front(); Q.pop();
inq[u.X][u.Y] = ;
if (mat[u.X][u.Y] == i) dp[u.X][u.Y] = min(dp[u.X][u.Y], d[u.X][u.Y]);
for (int j = ; j < ; j++) {
int x = u.X + dx[j], y = u.Y + dy[j];
if (x < || x >= n || y < || y >= m) continue;
if (d[x][y] > d[u.X][u.Y] + ) {
d[x][y] = d[u.X][u.Y] + ;
if (!inq[x][y]) inq[x][y] = , Q.push(mp(x, y));
}
}
}
}
}
printf("%d\n", dp[xt][yt]);
}
return ;
}
Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块的更多相关文章
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力
D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
题目大意: 给你一个n × m 的图,有p种宝箱, 每个点上有一个种类为a[ i ][ j ]的宝箱,a[ i ][ j ] 的宝箱里有 a[ i ][ j ] + 1的钥匙,第一种宝箱是没有锁的, ...
- Codeforces Round #355 (Div. 2) C. Vanya and Label 水题
C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题
B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...
- Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题
A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...
- Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~
B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #355 (Div. 2)C - Vanya and Label
啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor
菜菜菜!!!这么撒比的模拟题,听厂长在一边比比比了半天,自己想一想,然后纯模拟一下,中间过程检测一下,妥妥的就可以过. 题意:有N个东西要去搞碎,每个东西有一个高度,然后有一台机器支持里面可以达到的最 ...
- 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table
题目传送门 /* 水题:读懂题目就能做 */ #include <cstdio> #include <iostream> #include <algorithm> ...
随机推荐
- MySQL 数据类型 详解 (转载)
数值类型 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许我们指 ...
- 复习IOS多线程知识
线程的注意点 1.不要同时开太多的线程(1~3条线程即可,不要超过5条) 2.线程概念 * 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件 * 子线程 : 后台线程,异步线程 3.不要把 ...
- 根据DateTime来获取当天是周几(已完结)
只需要以下代码: @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(item.CreateTime. ...
- 依赖映射 Dependent Mapping
让一个类为其子类(泛意上的)执行DB映射 一些对象肯定会出现在另一对象的上下文中. 此时,使用另一对象的Mapper来执行第一个对象的映射,来简化映射过程. 运行机制 在DB持久化时,依赖者类依赖于所 ...
- javascript之基本包装类型(Boolean,Number,String)基础篇
前几天整理了javascript中Array方面的知识,但是String中的一些方法多多少少和Array里的方法有些类似容易混淆,就顺便连同String所在的包装类一起整理一下,希望可以帮助到初学者, ...
- 分享web前端七款HTML5 Loading动画特效集锦
以前我们大部分的Loading动画都是利用gif图片实现的,这种图片实现Loading动画的方法虽然也很不错,但是作为HTML5开发者来说,如果能利用HTML5和CSS3实现这些超酷的Loading动 ...
- Controller层的写法
项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对. 跳转到jsp页面: package com.controller; import java.i ...
- 南阳理工ACM975--关于521
http://acm.nyist.net/JudgeOnline/problem.php?pid=975 这是我的源码.一直超时,一直超时. 还有itoa函数函数的使用.可以改成sprintf(str ...
- c++学习——类成员的访问权限
成员的访问权限 Public: 任何人,尤其是那些要使用这个类库的客户程序员,都能访问那个紧跟在public 后面声明的成员. 默认的package: 在同一个目录里面的文件,并且都没有明确指明它是属 ...
- 设置Linux时间 同步时间
date命令将日期设置为2014年6月18日 ---- date -s 06/18/14 将时间设置为14点20分50秒 ---- date -s 14:20:50 将时间设置为2014年6月 ...