题解【CodeForces 910A The Way to Home】
题目大意
一只青蛙现在在一个数轴上,它现在要从点 \(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;
}
题解【CodeForces 910A The Way to Home】的更多相关文章
- [题解] Codeforces Round #549 (Div. 2) B. Nirvana
Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...
- 题解——CodeForces 438D The Child and Sequence
题面 D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input ...
- [题解][Codeforces]Good Bye 2019 简要题解
构造题好评,虽然这把崩了 原题解 A 题意 二人游戏,一个人有 \(k_1\) 张牌,另一个人 \(k_2\) 张,满足 \(2\le k_1+k_2=n\le 100\),每张牌上有一个数,保证所有 ...
- [题解][Codeforces]Codeforces Round #602 (Div. 1) 简要题解
orz djq_cpp lgm A 题意 给定一个分别含有 \(\frac n2\) 个左括号和右括号的括号序列 每次可以将序列的一个区间翻转 求一个不超过 \(n\) 次的操作方案,使得操作完之后的 ...
- [题解]Codeforces Round #709 (Div. 1, based on Technocup 2021 Final Round) - A. Basic Diplomacy
[题目] A. Basic Diplomacy [描述] Aleksey有n个朋友,有一个m天的假期,每天都需要一个朋友来陪他.给出每天有空的朋友的编号,要求同一个朋友来的天数不能超过m/2上取整.求 ...
- [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry
链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
- [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard
链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...
- [题解] Codeforces Global Round 22 1738 A B C D E F 题解
很久没rated打过cf的比赛了,这次打得还行,至少进前100了 点我看题 A. Glory Addicts 把类型0的数放进数组a里,类型1的数放进数组b里.如果\(|a|=|b|\),你可以把所有 ...
- 题解——Codeforces Round #508 (Div. 2) T3 (贪心)
贪心的选取最优解 然后相减好 记得要开long long #include <cstdio> #include <algorithm> #include <cstring ...
随机推荐
- python网络自动化ncclient模块,netconf协议检索与下发交换机配置
以juniper和华为设备为例 交换机必要配置,配置简单,使用ssh模式传输 #juniperset system services netconf ssh#华为 local-user netconf ...
- 归约与分组 - 读《Java 8实战》
区分Collection,Collector和collect 代码中用到的类与方法用红框标出,可从git库中查看 收集器用作高级归约 // 按货币对交易进行分组 Map<Currency, Li ...
- 好客租房44-react组件基础综合案例-5发表评论-1
发表评论 1给按钮绑定点击事件 2在事件处理程序中 通过state获取评论信息 3将评论信息添加到state中 并调用setState()方法更新数据 //导入react import React f ...
- C#实现登录功能(连接SQLServer数据库)
本例使用C#实现一个简单的登录功能,分为用户和管理员两个角色登录. 效果图: 核心代码 login.cs private void button1_Click(object sender, Event ...
- 将汇总结果导出到MySQL
①mysql建表test1 ②cd /opt/module/sqoop进入scoop路径 ③ bin/sqoop export \ > --connect jdbc:mysql://master ...
- Primal_Dual 原始对偶
不是费用流都需要用 SPFA 吗. 众所周知,SPFA 去世了,然后网络流显然有负边.于是我们可以像 Johnson 全源最短路一样,给边加上势能,具体实现看我之前的 博客 啦. 然后对于每一次跑 D ...
- 深入C++05:运算符重载
运算符重载 1.复数类 运算符重载目的:使对象运算表现得和编译器内置类型一样: 复数类例子 #include<iostream> using namespace std; class CC ...
- HashMap1.8常见面试问题
1.hashmap转红黑树的时机: for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNod ...
- linux目录结构及定时任务
1. Linux的根目录(最顶层的目录) windows系统有根目录:c盘的根目录就是c:\ d盘的根目录就是d:\ 每个盘(分区)都有自己的根目录 Linux系统, 也支持多个分区 Linux的分区 ...
- exe4j下载和使用
https://blog.csdn.net/weixin_44678104/article/details/101015065