【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 ...
随机推荐
- 如何在hadoop中使用外部的python程序文件
业务场景大概是这样,我需要在公司hadoop集群上对博文进行结巴分词.我的数据是存储在hive表格中的,数据量涉及到五百万用户三个月内发的所有博文. 首先对于数据来说,很简单,在hive表格中就是两列 ...
- Kubernetes-tutorials(五)
The tutorials use Katacoda to run a virtual terminal in your web browser that runs Minikube, a small ...
- codeforces 845A Chess Tourney
参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...
- SQL Server附加数据库拒绝访问错误解决方法
今天在MsSQL里附加数据库时提示操作系统错误5(拒绝访问),这里我没给出了两个解决方案供大家解决问题. 方案一:切换登录方式 出现这种情况是由于用“混合验证方式”(SQL Server身份验证)登录 ...
- thrift 调取 python php go 客户端代码
golang package main import ( "fmt" "git.apache.org/thrift.git/lib/go/thrift" &qu ...
- Github上的1000多本免费电子书重磅来袭!
Github上的1000多本免费电子书重磅来袭! 以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...
- Kotlin 0
#0 下载与配置: 官网上写的很详细,不再重复. #1 Hello world fun main(args: Array<String>) { println("Hello, w ...
- python 基础篇 13 迭代器与生成器
13. 前⽅⾼能-迭代器和⽣成器本节主要内容:1. 迭代器2. ⽣成器 ⼀. 迭代器我们之前⼀直在⽤可迭代对象进⾏迭代操作. 那么到底什么是可迭代对象. 本⼩节主要讨论可迭代对象. ⾸先我们先回顾⼀下 ...
- LeetCode - 20. Valid Parentheses(0ms)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- tomcat web.log 系统日志记录文件过大问题修改
目前各系统都是记录所有的日志,产生日志文件太大,按照如下设置修改log4j.properties文件:其中橙色部分为系统名称,例如water-scada系统,名称可以为scada. #Sun Jun ...