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 ...
随机推荐
- T-SQL 小全
--====================================================== ----数据库概念:创建.删除.使用数据库 ----================= ...
- Linux系统命令。
help:命令用于显示shell内部命令的帮助信息.help命令只能显示shell内部的命令 帮助信息.而对于外部命令的帮助信息只能使用man或者info命令查看 m ...
- Lexical or preprocessor 'XXX/XXX.h' issue file not found
最近做第三方登录,引入了第三库,结果就出来个这个问题.如下图所示: 刚开始编译运行都没问题,可下次再打开时就报这个错误…… 一个比较弱智的解决办法: 1. 删除第三方库文件(删除到垃圾箱,而且还要在文 ...
- 【译】尝试使用Nullable Reference Types
随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...
- 武林 HDU - 1107
题目链接:https://vjudge.net/problem/HDU-1107 注意:题目中只有两个不同门派的人在同一个地方才能对决,其他情况都不能对决. 还有,这步的有效的攻击只有走到下一步之后才 ...
- PHP版本的区别与用法详解
在我们安装PHP模块时,有时需要注意PHP编译的版本,下面讲解下PHP中VC6.VC9.TS.NTS版本的区别与用法详解,介绍php的两种执行方式. 1. VC6与VC9的区别:VC6版本是使用Vis ...
- 时间格式的字符串在ios中的转换问题
在移动端使用时间选择器的时候,选择了一个时间转换为时间戳,谷歌浏览器以及安卓手机使用 new Date( 选择的时间 ).getTime() 都能够拿到时间戳, 但是在ios手机上会出现出现NAN ...
- windiows下搭建python+selenium+unittest+Chrome的Web自动化环境
一.selenium.unittest概念 Selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架.它是一款用于运行端到端功能测试的超强工具.您可以使用多个编程语言编写测试,并且 ...
- 数据仓库系列之ETL过程和ETL工具
上周因为在处理很多数据源集成的事情一直没有更新系列文章,在这周后开始规律更新.在维度建模中我们已经了解数据仓库中的维度建模方法以及基本要素,在这篇文章中我们将学习了解数据仓库的ETL过程以及实用的ET ...
- 第一次接触Linux
一:文件目录操作命令 (一)创建文件 vim 文件名 按i进入插入模式 写完文件后,先按Esc, 再输入 :w ...