题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2259

相邻点之间连边权为1的边,就是水最短路了;

要注意点上的数不能改成负数,但是想一想改成负数还不如一开始就不走到这个点,所以这个不必担心;

注意1号点到2号点不能连边权为1的边,因为实际上不能直接走过去。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
int const xn=1e6+;
int n,hd[xn],ct,to[xn*],nxt[xn*],w[xn*];
ll dis[xn];
bool vis[xn];
struct N{
ll d; int id;
bool operator < (const N &y) const
{return d>y.d;}
};
priority_queue<N>q;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void add(int x,int y,int z){to[++ct]=y; nxt[ct]=hd[x]; w[ct]=z; hd[x]=ct;}
void dij()
{
memset(dis,0x3f,sizeof dis);
dis[]=; q.push((N){,});
while(q.size())
{
int x=q.top().id; q.pop();
if(vis[x])continue; vis[x]=;
for(int i=hd[x],u;i;i=nxt[i])
if(dis[u=to[i]]>dis[x]+w[i])
dis[u]=dis[x]+w[i],q.push((N){dis[u],u});
}
}
int main()
{
n=rd();
for(int i=,x;i<=n;i++)
{
x=rd();
if(i+x+<=n+)add(i,i+x+,); else add(i,n+,i+x-n);
if(i>)add(i,i+,);//i>1
if(i>)add(i,i-,);
}
dij();
printf("%lld\n",dis[n+]);
return ;
}

最新文章

  1. 配置 EPEL yum 源
  2. zookeeper启动后没有相关进程
  3. javascript脚本设置输入框只读的问题
  4. javascript将DOM事件处理程序封装为event.js 出现的低级错误记录
  5. List,set,Map 的用法和区别
  6. Iperf使用方法
  7. 一行一行分析JQ源码学习笔记-06
  8. Laravel安装及环境的配置(XAMPP集成开发环境下)
  9. 3D打印GCODE文件学习(一)
  10. 使用ajax实现form表单的submit事件
  11. JVM异常之:堆溢出OutofMemoryError
  12. Keil_uvision 基本使用教程
  13. python3之memcached
  14. centos6.5环境通过rpm包安装mysql5.5.51数据库
  15. Linux安装ElasticSearch-2.2.0
  16. Elasticsearch学习之深入搜索四 --- cross-fields搜索
  17. HTML5 Canvas ( 扩展context(&#39;2d&#39;) ) CanvasRenderingContext2D.prototype.你的方法名
  18. Codeforces1084 | Round526Div2 | 瞎讲报告
  19. Quartz2D-二维画图引擎 、自己定义UI控件
  20. Selenium自动化测试之数据驱动及用例管理

热门文章

  1. python etree解析xml
  2. CxImage新手教程,图文并茂
  3. 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。
  4. erlang中通过ip和子网掩码,计算地址范围 【二进制和十进制的转换】
  5. Gunicorn、Supervisor
  6. ASP.NET MVC4+BootStrap 实战(一)
  7. WPF之DataGrid篇:DataGridComboBoxColumn
  8. 矩阵十题【六】 poj3070 Fibonacci
  9. 【BZOJ4052】[Cerc2013]Magical GCD 乱搞
  10. angularcli填坑系列(持续更新...)