【NOIP模拟赛】chess 建图+spfa统计方案数
似乎弗洛伊德和迪杰斯特拉都干不了统计方案数,spfa的话就是不断入队就好。
#include <cstdio>
#include <cstring>
#include <queue>
#define get_hash(a,b) (a-1)*m+b
const int N=;
const int Inf=0x3f3f3f3f;
int t1[]={-,-,-,-,,,,},t2[]={-,-,,,,,-,-};
int f[N*N],dis[N*N],n,s[N][N],m,hash[N][N],S,E,all;
bool in[N*N],v[N][N],is[N*N],edge[N*N][N*N];
inline bool ok(int x,int y){
return x>&&x<=n&&y>&&y<=m&&s[x][y]!=;
}
struct V{
int to,next;
}c[N*N*N*N];
int head[N*N],t;
std::queue<int>q;
inline void add(int x,int y){
c[++t].to=y,c[t].next=head[x],head[x]=t;
}
void dfs(int x,int y){
if(v[x][y])return;
v[x][y]=;
for(int i=,p1,p2;i<;i++){
p1=x+t1[i],p2=y+t2[i];
if(ok(p1,p2)==false)continue;
if(s[p1][p2]==)dfs(p1,p2);
else is[hash[p1][p2]]=true;
}
}
void read_build(){
scanf("%d%d",&n,&m),all=n*m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&s[i][j]),hash[i][j]=get_hash(i,j);
if(s[i][j]==)S=hash[i][j];
if(s[i][j]==)E=hash[i][j];
}
for(int i=,p1,p2;i<=n;i++)
for(int j=;j<=m;j++)
if(s[i][j]!=&&s[i][j]!=)
if(s[i][j]==){
memset(is,,sizeof(is)),dfs(i,j);
for(int x=;x<=all;x++)
if(is[x]&&x!=E)
for(int y=;y<=all;y++)
if(x!=y&&is[y])
edge[x][y]=true;
}
else
for(int k=;k<;k++){
p1=t1[k]+i,p2=t2[k]+j;
if(ok(p1,p2)&&s[p1][p2]!=)
add(hash[i][j],hash[p1][p2]);
}
for(int i=;i<=all;i++)
for(int j=;j<=all;j++)
if(i!=j&&edge[i][j])
add(i,j);
}
void spfa_print(){
q.push(S),memset(dis,0x3f,sizeof(dis)),dis[S]=,f[S]=,in[S]=true;
while(!q.empty()){
int x=q.front();q.pop(),in[x]=false;
for(int i=head[x];i;i=c[i].next)
if(dis[x]+<dis[c[i].to]){
dis[c[i].to]=dis[x]+,f[c[i].to]=f[x];
if(in[c[i].to]==false)q.push(c[i].to),in[c[i].to]=true;
}
else if(dis[x]+==dis[c[i].to]){
f[c[i].to]+=f[x];
if(in[c[i].to]==false)q.push(c[i].to),in[c[i].to]=true;
}
}
if(f[E]==)printf("-1");
else printf("%d\n%d",dis[E]-,f[E]);
}
int main(){
read_build(),spfa_print();
return ;
}
【NOIP模拟赛】chess 建图+spfa统计方案数的更多相关文章
- NOIP模拟赛 6.29
2017-6-29 NOIP模拟赛 Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走, ...
- CH Round #55 - Streaming #6 (NOIP模拟赛day2)
A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...
- 11/1 NOIP 模拟赛
11.1 NOIP 模拟赛 期望得分:50:实际得分:50: 思路:暴力枚举 + 快速幂 #include <algorithm> #include <cstring> #in ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- CH Round #49 - Streaming #4 (NOIP模拟赛Day2)
A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...
- NOIP模拟赛-2018.11.6
NOIP模拟赛 今天想着反正高一高二都要考试,那么干脆跟着高二考吧,因为高二的比赛更有技术含量(我自己带的键盘放在这里). 今天考了一套英文题?发现阅读理解还是有一些困难的. T1:有$n$个点,$m ...
- Nescafe #29 NOIP模拟赛
Nescafe #29 NOIP模拟赛 不知道这种题发出来算不算侵权...毕竟有的题在$bz$上是权限题,但是在$vijos$似乎又有原题...如果这算是侵权的话请联系我,我会尽快删除,谢谢~ 今天开 ...
- NOI.AC NOIP模拟赛 第四场 补记
NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...
- 2016-06-19 NOIP模拟赛
2016-06-19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c ...
随机推荐
- Python基本图形绘制
turtle的一个画布空间最小单位是像素 turtle的绘制窗体:turtle.stup(width,heigth,startx,starty) 四个参数中后两个可选 turtle空间坐标体系:tur ...
- python系列7进程线程和协程
目录 进程 线程 协程 上下文切换 前言:线程和进程的关系图 由下图可知,在每个应用程序执行的过程中,都会去产生一个主进程和主线程来完成工作,当我们需要并发的执行的时候,就会通过主进程去生成一系列的 ...
- Windows Store App下代码加载page resource和resw文件里的string
加载page resource 在page的code behind里: this.Resources["textBoxStyle"] 加载resw文件里的string: Resou ...
- spring boot踩坑记
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...
- (长期更新)OI常用模板
代码很简单的模板就不收录了. DFT 离散傅立叶变换 void dft(pdd *a,int l,bool r){ int i,j=l/2,k; for(i=1;i<l;++i){ if(i&l ...
- Vue 去脚手架
上回模仿了一个nw,按照原理说,简单. 今天说Vue,脚手架是个好东西,做项目都给你配置好,不过对于我这种只想做一个界面的人来说,有点儿太大了,用不上. 如果说,不用脚手架要面临哪些问题呢. 1. 组 ...
- javascript代码规范 [转]
原文:http://www.css88.com/archives/5366 全局命名空间污染与 IIFE 总是将代码包裹成一个 IIFE(Immediately-Invoked Function Ex ...
- Java集合类面试题
java.util包中包含了一系列重要的集合类,而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 1.Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最 ...
- 超强排序JavaScript插件
Sortable:http://rubaxa.github.io/Sortable/
- 【实用】如何将sublime text 3 打造成实用的python IDE 环境
前段时间写脚本,一直使用的是pycharm ,无奈机器不配置实在不怎么样,我记得之前用过subline text,这是我用过的最酷炫的文本编辑器,参考了一下网上的文章,自己走了一些弯路,将心得写在这里 ...