题解 CF821D 【Okabe and City】
其实,这道题不用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】的更多相关文章
- CodeForces 821D Okabe and City
Okabe and City 题解: 将行和列也视为一个点. 然后从普通的点走到行/列的点的话,就代表这行/列已经被点亮了. 然后将费用为0的点建上边. 注意讨论(n,m)非亮的情况下. 代码: #i ...
- codeforces 821 D. Okabe and City(最短路)
题目链接:http://codeforces.com/contest/821/problem/D 题意:n*m地图,有k个位置是点亮的,有4个移动方向,每次可以移动到相邻的点亮位置,每次站在初始被点亮 ...
- CF821 D. Okabe and City 图 最短路
Link 题意:给出$n*m$大小的地图,已有$k$盏灯亮,人从左上角出发,右下角结束,期间必须走路灯点亮的地方,他可以在任意时刻消耗一枚硬币点亮一行或一列灯,他最多同时点亮一行或一列灯,要想点亮别的 ...
- 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 ...
- 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 ...
- 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 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 ...
- Codeforces Round #420 (Div. 2)
/*************************************************************************************************** ...
- Codeforces Round #420 (Div. 2) A-E
本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http: ...
随机推荐
- Spring环境搭建及简单demo
1. Spring框架简介(以下这段话可用于面试求职) Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为, IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器) ...
- 【codeforces 807C】Success Rate
[题目链接]:http://codeforces.com/contest/807/problem/C [题意] 给你4个数字 x y p q 要求让你求最小的非负整数b; 使得 (x+a)/(y+b) ...
- 清除eclipse中的SVN账号信息
清除eclipse中的SVN账号信息 参考了:http://blog.csdn.net/ningtieming/article/details/60469346 需要先在资源管理器中使用Tortois ...
- do while 循环和while循环的差别
do while 循环和while循环的差别 1.do while循环是先运行循环体,然后推断循环条件,假设为真,则运行下一步循环,否则终止循环. while循环是先推断循环条件,假设条件为真则 ...
- 17 facade
客户不须要内部的实现,仅仅须要知道有这个功能就好了,(最少知识原则)
- Android自己定义控件系列三:自己定义开关button(二)
接上一篇自己定义开关button(一)的内容继续.上一次实现了一个开关button的基本功能.即自己定义了一个控件.开关button,实现了点击切换开关状态的功能.今天我们想在此基础之上.进一步实现触 ...
- dns tunnel C&C
通过DNS控制主机以及执行命令 我的ubuntu 安装过程 1854 mkdir dns_tunnel_tool 1855 cd dns_tunnel_tool/ 1856 ls 1857 git c ...
- CodeForces--609C --Load Balancing(水题)
Load Balancing Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Subm ...
- shell脚本创建和执行
shell脚本并不能作为正式的编程语言,因为它是在Linux的shell中运行的,所以称他为shell脚本. 事实上,shell脚本就是一些命令的集合. 我们不妨吧所有的操作都记录到一个文档中,然后去 ...
- 对python变量的理解
#!/usr/bin/python class Person: '''some words content or descriptions!''' name='luomingchuan' _age = ...