CodeForces 821D Okabe and City
题解:
将行和列也视为一个点。 然后从普通的点走到行/列的点的话,就代表这行/列已经被点亮了。
然后将费用为0的点建上边。
注意讨论(n,m)非亮的情况下。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
const int M = 6E5 + ;
int x[N], y[N];
vector<int> vx[N], vy[N];
bool cmpx(int a, int b){
return y[a] < y[b];
}
bool cmpy(int a, int b){
return x[a] < x[b];
}
int head[N], to[M], nt[M], ct[M], tot;
void add(int u, int v, int w){
to[tot] = v; ct[tot] = w;
nt[tot] = head[u]; head[u] = tot++;
}
priority_queue<pll, vector<pll>, greater<pll> > pq;
int dis[N];
int main(){
memset(head, -, sizeof head);
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int f = , b;
for(int i = ; i <= k; ++i){
scanf("%d%d", &x[i], &y[i]);
if(x[i] == n && y[i] == m) f = i;
if(x[i] == && y[i] == ) b = i;
vx[x[i]].pb(i);
vy[y[i]].pb(i);
}
for(int i = ; i <= n; ++i){
sort(vx[i].begin(), vx[i].end(), cmpx);
for(int j = ; j < vx[i].size(); ++j){
int p = vx[i][j-], n = vx[i][j];
if(y[n] == y[p] + ) {
add(n, p, );
add(p, n, );
}
}
for(int x : vx[i]){
if(i>) add(x, k+i-, );
add(x, k+i, );
if(i<n) add(x, k+i+, );
}
for(int x : vx[i]){
add(k+i, x, );
}
if(i > ){
for(int x : vx[i-]){
add(k+i, x, );
}
}
if(i < n){
for(int x : vx[i+]){
add(k+i, x, );
}
}
}
for(int i = ; i <= m; ++i){
sort(vy[i].begin(), vy[i].end(), cmpy);
for(int j = ; j < vy[i].size(); ++j){
int p = vy[i][j-], n = vy[i][j];
if(x[n] == x[p] + ){
add(n, p, );
add(p, n, );
}
}
for(int x : vy[i]){
if(i > ) add(x, k+n+i-, );
add(x, k+n+i, );
if(i < m) add(x, k+n+i+, );
}
for(int x : vy[i]){
add(k+i+n, x, );
}
if(i > ){
for(int x : vy[i-]){
add(k+i+n, x, );
}
}
if(i < n){
for(int x : vy[i+]){
add(k+i+n, x, );
}
}
}
if(!f){
f = k++n+m;
add(k+n, f, );
add(k+n+m, f, );
}
memset(dis, inf, sizeof dis);
pq.push({,b});/// id, c, cost
dis[b] = ;
while(!pq.empty()){
pll now = pq.top();
pq.pop();
int u = now.se, cost = now.fi;
if(dis[u] != cost) continue;
if(u == f){
cout << cost << endl;
return ;
}
for(int i = head[u]; ~i; i = nt[i]){
int v = to[i], w = ct[i];
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
pq.push({dis[v], v});
}
}
}
puts("-1");
return ;
}
CodeForces 821D Okabe and City的更多相关文章
- Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- codeforces 821 D. Okabe and City(最短路)
题目链接:http://codeforces.com/contest/821/problem/D 题意:n*m地图,有k个位置是点亮的,有4个移动方向,每次可以移动到相邻的点亮位置,每次站在初始被点亮 ...
- Codeforces 821C - Okabe and Boxes
821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...
- CF821 D. Okabe and City 图 最短路
Link 题意:给出$n*m$大小的地图,已有$k$盏灯亮,人从左上角出发,右下角结束,期间必须走路灯点亮的地方,他可以在任意时刻消耗一枚硬币点亮一行或一列灯,他最多同时点亮一行或一列灯,要想点亮别的 ...
- codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...
- codeforces B. Strongly Connected City(dfs水过)
题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向, 问是否每一个节点都可以由其他的节点到达.... 思路:规律没有想到,直接爆搜!每一个节点dfs ...
- Codeforces 821E Okabe and El Psy Kongroo
题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y-1)或者(x+1,y)或者(x+1,y+1)三个位子之一.现在一共有N段线段,每条线段都是平行于X ...
- Codeforces 821C Okabe and Boxes(模拟)
题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...
- Codeforces 521 E cycling city
cf的一道题,非常有意思,题目是问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 其实就是判断一下是否为仙人掌就行了,如果不是仙人掌的话肯定就存在,题目难在输出路径上,改了半 ...
随机推荐
- 【iOS】设置 rootViewController
iOS 开发中,rootViewController 经常用到,示例代码如下: self.window = [[UIWindow alloc] initWithFrame:[UIScreen main ...
- Python—推导式
推导式 推导式:comprehensions(又称解析式),是Python的一种独有特性,相当于语法糖的存在,推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2 ...
- 请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天。
请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天. #include <stdio.h> int main() { /* 定义需要计算的日期 ...
- nginx lua集成kafka
NGINX lua集成kafka 第一步:进入opresty目录 [root@node03 openresty]# cd /export/servers/openresty/ [root@node03 ...
- Android使用com.google.android.cameraview.CameraView进行拍照
import android.Manifest;import android.annotation.SuppressLint;import android.content.Context;import ...
- 带你剖析WebGis的世界奥秘----Geojson数据加载(高级)
前言:前两周我带你们分析了WebGis中关键步骤瓦片加载+点击事件(具体的看前两篇文章),下面呢,我带大家来看看Geojson的加载及其点击事件 Geojson数据解析 GeoJSON是一种对各种地理 ...
- DT-06 For Homekit
一. 配置DT-06上网 连接此热点,会自动弹出wifi配置页面. 输入选中的路由密码,点 Join加入,如果路由没有出现在列表中,点 Other手工输入(仅支持2.4g路由配置) 二.配置dt-06 ...
- 优雅的在WinForm/WPF/控制台 中使用特性封装WebApi
优雅的在WinForm/WPF/控制台 中使用特性封装WebApi 说明 在C/S端作为Server,建立HTTP请求,方便快捷. 1.使用到的类库 Newtonsoft.dll 2.封装 HttpL ...
- Mac 打造开发工作环境
近日公司配的dell笔记本越来越难担重任(主要是CPU太差,本人是Java开发,IDE一编译CPU就100%),于是狠下心入手了一台常规顶配Macbook Pro,现记录新本本的调教过程. Homeb ...
- node实现后台权限管理系统
本文面向的是node初学者,目标是搭建一个基础的后台权限系统.使用的node框架是上手最简单的express,模板是ejs,这些在node入门的书籍中都有介绍说明,所以应该是难度较低的. 对于node ...