似乎弗洛伊德和迪杰斯特拉都干不了统计方案数,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统计方案数的更多相关文章

  1. NOIP模拟赛 6.29

    2017-6-29 NOIP模拟赛 Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走, ...

  2. CH Round #55 - Streaming #6 (NOIP模拟赛day2)

    A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...

  3. 11/1 NOIP 模拟赛

    11.1 NOIP 模拟赛 期望得分:50:实际得分:50: 思路:暴力枚举 + 快速幂 #include <algorithm> #include <cstring> #in ...

  4. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  5. CH Round #49 - Streaming #4 (NOIP模拟赛Day2)

    A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...

  6. NOIP模拟赛-2018.11.6

    NOIP模拟赛 今天想着反正高一高二都要考试,那么干脆跟着高二考吧,因为高二的比赛更有技术含量(我自己带的键盘放在这里). 今天考了一套英文题?发现阅读理解还是有一些困难的. T1:有$n$个点,$m ...

  7. Nescafe #29 NOIP模拟赛

    Nescafe #29 NOIP模拟赛 不知道这种题发出来算不算侵权...毕竟有的题在$bz$上是权限题,但是在$vijos$似乎又有原题...如果这算是侵权的话请联系我,我会尽快删除,谢谢~ 今天开 ...

  8. NOI.AC NOIP模拟赛 第四场 补记

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  9. 2016-06-19 NOIP模拟赛

          2016-06-19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c ...

随机推荐

  1. js实现监听浏览器窗口大小改变事件

    window.onresize = function(){   }

  2. Redis缓存数据库的安装与配置(1)

    1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...

  3. HDU暑假多校第八场J-Taotao Picks Apples

    一.题意 给定一个序列,之后给出若干个修改,修改的内容为在原序列的基础上,将某一位元素的值改成给定的值<每次修改相互独立,不保存修改后的结果>.之后询问,在选择第一位元素的情况下,最长递增 ...

  4. Eclipse报错:An internal error occurred during: "Building workspace". Java heap space),卡死解决办法

    在项目工程的根目录下,找到.project,用记事本打开,把两处删除掉: 第一处: <buildCommand> <name>org.eclipse.wst.jsdt.core ...

  5. 5-sql语句

    1 [oracle@ocp ~]$ . oraenv # ORACLE_SID = [oracle] ? orcl The Oracle base has been set to /u01/app/o ...

  6. 用intellij Idea加载eclipse的maven项目全流程

    eclipse的maven项目目录 全流程 加载项目 打开intellij Idea file -> new -> module from existing Sources  选择.pom ...

  7. valgrind检查still reachable情况

    valgrind --leak-check=yes检查bufr编解码程序运行时提示still reachable: 568 bytes in 1 blocks,如下图示: 于是怀疑有内存泄漏,难道是m ...

  8. jsp 路径问题和环境路径以及各种路径总结

    首先确定问题: 浏览器发送请求后,服务器会返回一个响应,但是返回的网页中,会有各种路径问题,所以在此用jsp中的属性来解决.(只是记录问题,用了不专业的术语,请见谅.) 总结: 以路径  http:/ ...

  9. 「日常训练」 不容易系列之(3)—— LELE的RPG难题 (HDU 2045)

    题目简述 有排成一行的n" role="presentation">nn个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何 ...

  10. 不同浏览器css引入外部字体的方式

    /** * 字体后缀和浏览器有关,如下所示 * .TTF或.OTF,适用于Firefox 3.5.Safari.Opera * .EOT,适用于Internet Explorer 4.0+ * .SV ...