洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】
和bzoj同名题不一样!
起点和水点向花费一个荷花能到的第一个点连一条边权为1的有向边,然后跑计数spfa即可
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=35,dx[]={-1,-1,1,1,-2,-2,2,2},dy[]={-2,2,-2,2,-1,1,-1,1};
int n,m,a[N][N],id[N][N],tot,h[N*N],cnt,s,t,dis[N*N],vis[N][N],ti;
long long b[N*N];
bool v[N*N];
struct qwe
{
int ne,to;
}e[500005];
void add(int u,int v)
{//cerr<<u<<" "<<v<<endl;
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
inline bool ok(int x,int y)
{
return x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]!=2&&vis[x][y]!=ti;
}
void dfs(int u,int x,int y,int f)
{//cerr<<u<<" "<<x<<" "<<y<<endl;
vis[x][y]=ti;
if((a[x][y]==4||a[x][y]==0)&&!f)
{
add(u,id[x][y]);
// add(id[x][y],u);
return;
}
for(int i=0;i<8;i++)
if(ok(x+dx[i],y+dy[i]))
dfs(u,x+dx[i],y+dy[i],0);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
id[i][j]=++tot;
if(a[i][j]==3)
s=id[i][j];
if(a[i][j]==4)
t=id[i][j];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]!=2&&a[i][j]!=1)
ti++,dfs(id[i][j],i,j,1);
queue<int>q;
for(int i=1;i<=tot;i++)
dis[i]=1e9;
dis[s]=0;
b[s]=1;
v[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
v[u]=0;
for(int i=h[u];i;i=e[i].ne)
{
if(dis[e[i].to]>dis[u]+1)
{
dis[e[i].to]=dis[u]+1;
b[e[i].to]=b[u];
if(!v[e[i].to])
{
v[e[i].to]=1;
q.push(e[i].to);
}
}
else if(dis[e[i].to]==dis[u]+1)
{
b[e[i].to]+=b[u];
if(!v[e[i].to])
{
v[e[i].to]=1;
q.push(e[i].to);
}
}
}
}
if(dis[t]==1e9)
puts("-1");
else
printf("%d\n%lld\n",dis[t]-1,b[t]);
return 0;
}
洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】的更多相关文章
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- 【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解
题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点 ...
- 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...
- Dfs+Spfa【p1606】[USACO07FEB]荷叶塘Lilypad Pond
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925
最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 【题解】洛谷P1073 [NOIP2009TG] 最优贸易(SPFA+分层图)
次元传送门:洛谷P1073 思路 一开始看题目嗅出了强连通分量的气息 但是嫌长没打 听机房做过的dalao说可以用分层图 从来没用过 就参考题解了解一下 因为每个城市可以走好几次 所以说我们可以在图上 ...
随机推荐
- Thinkphp5学习 Windows下的安装
方法一.通过官方网站直接下载: (1)下载地址:http://www.thinkphp.cn/down.html: (2)下载后,解压到web目录下: (3)访问:http://localhost/目 ...
- http_load分析(转)
http://www.cnblogs.com/xuning/p/3954057.html 一.前言 http_load是一款测试web服务器性能的开源工具,从下面的网址可以下载到最新版本的http_l ...
- POJ 2502 【思维是朴素的最短路 卡输入和建图】
题意: 给出两个坐标,分别是小明家和小明学校的坐标. 给出多条地铁线,给出每站的坐标,已知地铁是双向的,每条线以-1 -1结尾. 给出地铁速度,步行速度. 地铁线可看成是顺次连接的线段. 求小明从家到 ...
- POJ 1511 【heap+dij】
题意: t组样例. 每组有n个节点,有m条单向边. 有m组输入,每组a b c 表示从a到b的单向边的权值是c. 求解,从编号为1的节点出发,有n-1个人,要求他们分别到达编号从2到n的节点再返回,所 ...
- IDEA下使用protobuf2(java)
目录 一.介绍 二.特点 三.结构 四.选择版本 五.Intellij IDEA中使用Protobuf 1.下载个protoc.exe 2.编辑个.proto文件 3.将.proto文件转成Java类 ...
- mybatis结合generator进行分页插件PluginAdapter开发
使用org.mybatis.generator生成UserExample时,无法进行分页,使用下面这个类运行generator便可以生成分页相关的属性了 package org.mybatis.gen ...
- 分布式 OLTP 数据库
PingCAP招聘职位-拉勾网-最专业的互联网招聘平台 分布式 OLTP 数据库
- arcgis安装路径的获得
//Get the ArcGIS install location string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path; / ...
- jenkins修改日志级别方法
1.jenkins日志有时候也会消耗掉很大内存,在传输时也会消耗掉大量带宽,如图,300+M的日志大小,太夸张了吧 2.修改日志级别的方法: 在配置文件里修改,重启后永久生效,配置路径:/etc/sy ...
- FM算法及FFM算法
转自:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html http://blog.csdn. ...