其实,这道题不用long long也能AC。


题意是给你一个矩阵,有一些格子被点亮有一些没有,每一次只能在被点亮的格子上面走。

然后你每一次都可以选择点亮一行或一排(非永久),现在问你最少点多少次可以走到终点?


思路十分好想。

我们把相邻的格子边权设为0,把不相邻但只差一行的格子之间边权设为1。(其他情况都不能直接到达)

然后跑一下SPFA就可以了。

但是需要考虑一个特例:终点有没有被点亮。

如果点了的话那没关系,没点的话得把(n+1,m+1)设为点亮的点。(要不然我们走不到终点不是吗)


AC代码如下:

11149ms/192kb

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
namespace StandardIO{
template<typename T>inline void read(T &x){
x=0;T f=1;char c=getchar();
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
x*=f;
}
template<typename T>inline void write(T x){
if(x<0)putchar('-'),x*=-1;
if(x>=10)write(x/10);
putchar(x%10+'0');
}
}
using namespace StandardIO;
namespace Solve{
const int N=10100;
const int INF=0x3f3f3f3f;
int n,m,k;
struct node{
int from,to;
}edge[N];
int dis[N],vis[N];
inline int spfa(){
for(register int i=1;i<=k;++i)dis[i]=INF;
queue<int>q;q.push(1);
vis[1]=1,dis[1]=0;
while(!q.empty()){
int to=q.front();q.pop();
vis[to]=0;
for(register int i=1;i<=k;++i){
if(to==i)continue;
int val=INF;
int dx=abs(edge[to].from-edge[i].from),dy=abs(edge[to].to-edge[i].to);
if(dx+dy==1)val=0;
else if(dx<=2||dy<=2)val=1;
if(dis[i]>dis[to]+val){
dis[i]=dis[to]+val;
if(!vis[i]){
q.push(i),vis[i]=1;
}
}
}
}
if(dis[k]!=INF)return dis[k];
return -1;
}
inline void solve(){
read(n),read(m),read(k);
int flag=0;
for(register int i=1;i<=k;++i){
read(edge[i].from),read(edge[i].to);
if(edge[i].from==n&&edge[i].to==m)flag=1;
}
if(!flag)edge[++k].from=n+1,edge[k].to=m+1;
write(spfa());
}
}
using namespace Solve;
int main(){
solve();
}

题解 CF821D 【Okabe and City】的更多相关文章

  1. CodeForces 821D Okabe and City

    Okabe and City 题解: 将行和列也视为一个点. 然后从普通的点走到行/列的点的话,就代表这行/列已经被点亮了. 然后将费用为0的点建上边. 注意讨论(n,m)非亮的情况下. 代码: #i ...

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

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

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

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

  4. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo DP+矩阵快速幂加速

    E. Okabe and El Psy Kongroo     Okabe likes to take walks but knows that spies from the Organization ...

  5. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

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

  7. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂

    E. Okabe and El Psy Kongroo   Okabe likes to take walks but knows that spies from the Organization c ...

  8. Codeforces Round #420 (Div. 2)

    /*************************************************************************************************** ...

  9. Codeforces Round #420 (Div. 2) A-E

    本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...

随机推荐

  1. spring boot的几种配置类型

    1.spring boot的几种配置类型 1)基本配置,spring自动读取的,全都在application.yml里配置,spring会自动读取这个配置文件 2)个性化配置:比如配置intercep ...

  2. NHibernate之旅(18):初探代码生成工具使用

    本节内容 引入 代码生成工具 结语 引入 我们花了大量的篇幅介绍了相关NHibernate的知识.一直都是带着大家手动编写代码,首先创建数据库架构.然后编写持久化类和映射文件,最后编写数据操作方法.測 ...

  3. HDU 1215

    由算术基本定理, 直接使用公式就好 #include <iostream> #include <cstdio> #include <algorithm> #incl ...

  4. POJ 1284

    想了很久,只想到枚举的方法,估计会超时吧. 原来有这样一条性质:p为素数,则p有phi(p-1)个原根 Orz... #include <iostream> #include <cs ...

  5. HDU 3861--The King’s Problem【scc缩点构图 &amp;&amp; 二分匹配求最小路径覆盖】

    The King's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP

    B. Longtail Hedgehog   This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...

  7. 33.Qt模型与视图

    #include "mainwindow.h" #include <QApplication> #include <QAbstractItemModel> ...

  8. Spark RDD概念学习系列之如何创建RDD

    不多说,直接上干货! 创建RDD 方式一:从集合创建RDD (1)makeRDD (2)Parallelize 注意:makeRDD可以指定每个分区perferredLocations参数,而para ...

  9. 【转自网络】JS实现保存当前网页HTML到本地

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Struts2简单环境搭建

    一.开篇 Struts2是一个运行于web容器的表示层框架,其核心作用是帮助我们处理Http请求.Struts2处理Http请求(Request),并进行内部处理,再进行Http返回. 下载strut ...