题解【CodeForces 910A The Way to Home】
2024-09-08 02:28:02
题目大意
一只青蛙现在在一个数轴上,它现在要从点 \(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;
}
最新文章
- hibernate(1) —— 入门
- OpenJudge 2985数字组合 解析报告/DP
- Codeforces Round #374 (div.2)遗憾题合集
- activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
- Windows phone 8 学习笔记(4) 应用的启动(转)
- How to say ";no";?
- Linux学习笔记31——网络信息
- 破解官方recovery的签名验证
- Android网络下载图片
- Autofac in webapi2
- JavaScript内置的预定义函数
- ubuntu14.04 64位 安装JDK1.7
- 拿到月薪30K,必选一些Python好书!
- SSM框架+MySql保存emoji表情
- Chapter 4 Invitations——11
- JavaScript遍历集合(for...of/for...in/forEach)
- css.aa
- Vagrant测试
- 转 这种方法可以免去自己计算大文件md5 的麻烦
- 1085 PAT单位排行