题目大意

一只青蛙现在在一个数轴上,它现在要从点 \(1\) 跳到点 \(n\) ,它每次可以向右跳不超过 \(d\) 个单位。比如,它可以从点 \(x\) 跳到点 \(x+a\)(\(1\le a\le d\)) 。

特别的,青蛙只能在有百合花的点上停留。保证点 \(1\) 和点 \(n\) 之间有一些点有百合花,并且起点和终点有百合花。请输出青蛙到达点 \(n\) 的最小跳跃次数。

题解

注意到 \(n\le 100\),暴力将 \(x\to x+a\) 建图跑最短路即可。

#include<iostream>
#include<ctime>
#include<queue>
#include<stack>
#include<cmath>
#include<iterator>
#include<cctype>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2001,INF=0x3f3f3f3f;
typedef long long ll;
typedef vector<pair<int,ll> > graph[N];
graph g;
inline void addedge(int u,int v,ll w){g[u].push_back(make_pair(v,w));}
int n,d,dis[N];
bool vis[N];
string str;
struct node
{
int idx,dis;
node(int _idx,int _dis){idx=_idx; dis=_dis;}
inline bool operator<(const node& w)const{return dis>w.dis;}
};
inline void dij(int s)
{
memset(dis,0x3f,sizeof dis);
priority_queue<node> q; q.push(node(s,0)); dis[s]=0;
while (!q.empty())
{
node now=q.top(); q.pop(); int nowi=now.idx,S=g[nowi].size();
if (vis[nowi]) continue; vis[nowi]=true;
for (int i=0;i<S;i++)
{
int v=g[nowi][i].first,w=g[nowi][i].second;
if (dis[nowi]+w<dis[v]){dis[v]=dis[nowi]+w; if (!vis[v]) q.push(node(v,dis[v]));}
}
}
}
int main()
{
scanf("%d%d",&n,&d); cin>>str;
for (int i=0;i<n;i++)
if (str[i]-'0')
for (int j=1;j<=d;j++)
{
int pos=i+j; if ((pos>n)||(pos==i)||(str[pos]=='0')) continue;
addedge(i+1,pos+1,1);
}
dij(1); printf("%d",(dis[n]==INF)?-1:dis[n]);
return 0;
}

最新文章

  1. hibernate(1) —— 入门
  2. OpenJudge 2985数字组合 解析报告/DP
  3. Codeforces Round #374 (div.2)遗憾题合集
  4. activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
  5. Windows phone 8 学习笔记(4) 应用的启动(转)
  6. How to say &quot;no&quot;?
  7. Linux学习笔记31——网络信息
  8. 破解官方recovery的签名验证
  9. Android网络下载图片
  10. Autofac in webapi2
  11. JavaScript内置的预定义函数
  12. ubuntu14.04 64位 安装JDK1.7
  13. 拿到月薪30K,必选一些Python好书!
  14. SSM框架+MySql保存emoji表情
  15. Chapter 4 Invitations——11
  16. JavaScript遍历集合(for...of/for...in/forEach)
  17. css.aa
  18. Vagrant测试
  19. 转 这种方法可以免去自己计算大文件md5 的麻烦
  20. 1085 PAT单位排行

热门文章

  1. axios源码解析 - 请求方法的别名实现
  2. yarn/npm 设置镜像地址
  3. AI趋势量化系统(Binance升级版)
  4. ELK 是什么?
  5. VBA驱动SAP GUI实现办公自动化(一)
  6. Centos 创建新的虚拟环境
  7. 安装@parcel/transformer-image注意的问题
  8. 【Parcel 2 + Vue 3】从0到1搭建一款极快,零配置的Vue3项目构建工具
  9. ABAP CDS - DEFINE VIEW, view_annot
  10. 手把手教你用 Python 下载手机小视频