CodeForces 677D. Vanya and Treasure 枚举行列
题意:
给定一张n*m的图,图上每个点标有1~p的值,你初始在(1,1)点,你必须按照V:1,2,3...p的顺序走图上的点,问你如何走时间最少。
思路:
我一开始想的思路感觉很巧妙,但是TLE了。就是把不同值的点放在不同的vector中,然后类似dp的从2更新最小距离到p。因为我这是暴力枚举点的,复杂度不对。后来发现这个思路还需要优化一下,就是把同一行的点放在一起,for一遍这一行属于V的点,就可以更新本行的信息了,再向下把列中属于v+1的更新了。复杂度就降到了O(n^3);
%和学习的是这份代码:huhuhu
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
///#pragma GCC optimize(3)
///#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; ///priority_queue<int> q;//这是一个大根堆q
///priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
///#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
///priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //
const int mod = 1e9+; const double PI=acos(-1.0); // #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/ const int maxn = ;
int dp[maxn][maxn], mp[maxn][maxn];
vector<pii>v[maxn*maxn];
vector<int>col[maxn];
bool vis[maxn];
int main(){
int n,m,tp;
scanf("%d%d%d", &n, &m, &tp);
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
int op; scanf("%d", &op);
mp[i][j] = op;
if(op==)dp[i][j] = i+j-;
else dp[i][j] = inf;
v[op].pb(pii(i,j));
}
} for(int i=; i<tp; i++){
for(int j=; j<= max(n,m); j++){
col[j].clear();
vis[j] = false;
} for(pii p : v[i+]){
col[p.se].pb(p.fi);
}
for(pii p: v[i]){
vis[p.fi] = true;
} for(int j=; j<=n; j++) if(vis[j]){
int best = inf;
for(int k = ; k<=m; k++){
best++;
if(mp[j][k] == i)
best = min(best, dp[j][k]);
for(int y : col[k]){
dp[y][k] = min(dp[y][k], best + abs(y-j));
}
} best = inf;
for(int k = m; k>=; k--){
best++;
if(mp[j][k] == i)
best = min(best, dp[j][k]);
for(int y : col[k]){
dp[y][k] = min(dp[y][k], best + abs(y-j));
}
}
}
} for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
if(mp[i][j]==tp){
printf("%d\n", dp[i][j]);
}
}
}
return ;
}
CF-677D
CodeForces 677D. Vanya and Treasure 枚举行列的更多相关文章
- Codeforces 677D Vanya and Treasure 暴力+BFS
链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- CodeForces 677D Vanya and Treasure
$dp$,树状数组. 很明显这是一个$DAG$上的$dp$,由于边太多,暴力$dp$会超时,需要优化. 例如计算$dp[x][y]$,可以将区域分成四块,$dp[x][y]$取四块中的最小值,每一块用 ...
- codeforces 677D D. Vanya and Treasure(二维线段树)
题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...
- 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 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- Codeforces 677E Vanya and Balloons
Vanya and Balloons 枚举中心去更新答案, 数字过大用log去比较, 斜着的旋转一下坐标, 然后我旋出来好多bug.... #include<bits/stdc++.h> ...
- codeforces 677D(分层图dp)
Codeforces 677D 传送门:https://codeforces.com/contest/677/problem/D 题意: 给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐 ...
- 题解-CF677D Vanya and Treasure
CF677D Vanya and Treasure 有一个 \(n\times m\) 的矩阵 \(a(1\le a_{i,j}\le p)\),求从起点 \((1,1)\) 出发依次遍历值为 \(1 ...
随机推荐
- 【iOS】ERROR ITMS-90032: "Invalid Image Path...
用 Application Loader 提交苹果审核时出现了这个问题,具体如下: ERROR ITMS-: "Invalid Image Path - No image found at ...
- python 处理json数据
python 处理 json数据 以下是登录账号后获取的json数据,headers中注意加入cookie值 需要处理的数据如下: 全部代码如下 #!/usr/bin/env python # -*- ...
- Svn提交冲突问题
MEclipse中的svn冲突解决办法: 1. 点击提交,报错——‘SVN提交’has encountered a problem. 2. 选中无法提交的文件,点击更新操作 ...
- fiddler设置断点
1.有两种方法设置断点 before response:也就是发送请求之后,但是Fiddler代理中转之前,这时可以修改请求的数据 after response:也就是服务器响应之后,但是在Fiddl ...
- Windows to Linux API 映射
- 基于opencv,开发摄像头播放程序
前言 Windows下实现摄像视频捕捉有多种实现方式:各种方式的优劣,本文不做对比.但是,opencv是一款老牌开发库,在图像处理领域声名显赫.采用opencv来处理摄像视频,在性能和稳定性上,是有保 ...
- 在MAC终端下打开Finder:
在Terminal中打开Finder: open . 在Finder中打开Terminal: 系统偏好设置 -> 键盘 -> 快捷键 -> 服务,勾选「新建位于文件夹位置的终端窗口」
- [原创实践]RedHat Enterprise Linux 5 安装GCC和redis
Redis的安装需要使用GCC,Red Hat Enterprise 5默认是不安装gcc的,需要自己手动安装. 1:查看系统中是否有gcc gcc -v 查看本机linux版本 lsb_releas ...
- 关于JSON解析的问题(js序列化及反序列化)
我们都知道,现在的开发模式都是前后端分离的,后台返回数据给前端,前端负责数据交互并渲染到页面,所以我们需要从后端接口上获取数据显示到页面上.在接受服务器端数据数据时,一般是字符串.这时,就需要用到JS ...
- JAVA的初体验
jdk的地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 第一步:找到相应的JAVA SE版本,点击down ...