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的更多相关文章

  1. 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 ...

  2. codeforces 821 D. Okabe and City(最短路)

    题目链接:http://codeforces.com/contest/821/problem/D 题意:n*m地图,有k个位置是点亮的,有4个移动方向,每次可以移动到相邻的点亮位置,每次站在初始被点亮 ...

  3. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  4. CF821 D. Okabe and City 图 最短路

    Link 题意:给出$n*m$大小的地图,已有$k$盏灯亮,人从左上角出发,右下角结束,期间必须走路灯点亮的地方,他可以在任意时刻消耗一枚硬币点亮一行或一列灯,他最多同时点亮一行或一列灯,要想点亮别的 ...

  5. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  6. codeforces B. Strongly Connected City(dfs水过)

    题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向, 问是否每一个节点都可以由其他的节点到达.... 思路:规律没有想到,直接爆搜!每一个节点dfs ...

  7. 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 ...

  8. Codeforces 821C Okabe and Boxes(模拟)

    题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...

  9. Codeforces 521 E cycling city

    cf的一道题,非常有意思,题目是问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 其实就是判断一下是否为仙人掌就行了,如果不是仙人掌的话肯定就存在,题目难在输出路径上,改了半 ...

随机推荐

  1. python编码问题——解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    python实现爬虫遇到编码问题: error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX ...

  2. BrowserSync,自动刷新,解放F5,去掉更新提示

    BrowserSync虽然这个技术不算新,但是依然有用.略微介绍下 没有安装node,先安装node,这里不再做介绍 安装 npm install -g browser-sync  全局安装,方便在任 ...

  3. Ubuntu下python安装mysqldb(驱动)

    最近在学习Django框架,需要使用到数据库,我使用的是mysql,跟java一样,需要安装驱动,这是驱动的下载网址http://sourceforge.net/projects/mysql-pyth ...

  4. Tomcat+MySQL常见调优参数

    一.Tomcat 调优 (一).Tomcat内存优化 参数一: vim /tomcat/bin/catalina.sh CATALINA_OPTS="-server -Xms128m -Xm ...

  5. python_0基础开始_day05

    第五节 一.字典 python的数据结构之一 字典 —— dict 定义:dic = {"key":"dajjlad"} 作用:存储数据,大量,将数据和数据起到 ...

  6. 佳木斯集训Day4

    Day4的出题人好毒瘤啊!!! T1我打表过的,正解现在也不会 #include <bits/stdc++.h> #define MAXN 10050 #define ll long lo ...

  7. hadoop学习(六)----HDFS的shell操作

    HDFS所有命令: [uploaduser@rickiyang ~]$ hadoop fs Usage: hadoop fs [generic options] [-appendToFile < ...

  8. Powered by .NET Core 进展:用 docker-compose 验证高并发问题嫌疑犯 docker swarm

    相关博文: [故障公告]发布 .NET Core 版博客站点引起大量 500 错误 [网站公告].NET Core 版博客站点第二次发布尝试 暴风雨中的 online : .NET Core 版博客站 ...

  9. Flink 源码解析 —— Flink JobManager 有什么作用?

    JobManager 的作用 https://t.zsxq.com/2VRrbuf 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...

  10. Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations

    一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...