# [Oibh] bzoj2259 新型计算机 [最短路]

Description:

Solution:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e6 + 5;
struct edge {
int nxt, to, w;
} e[maxn * 5];
int n, cnt = 1;
int h[maxn], nxt[maxn], pre[maxn], d[maxn];
void link(int u, int v, int w) {
e[++cnt].nxt = h[u];
h[u] = cnt;
e[cnt].to = v;
e[cnt].w = w;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
for(int j = i; j <= min(i + x + 1, n) && !pre[j]; ++j) {
pre[j] = 1;
}
for(int j = i + x + 1; j <= n && !nxt[j]; ++j) {
nxt[j] = 1;
}
if(i + x + 1 <= n + 1) {
link(i, i + x + 1, 0);
} else {
link(i, n + 1, i + x - n);
}
}
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push(make_pair(0, 1));
memset(d, 0x3f3f, sizeof(d));
d[1] = 0;
while(!q.empty()) {
pair<int, int> o = q.top();
q.pop();
int u = o.second;
for(int i = h[u]; i; i = e[i].nxt) {
if(d[e[i].to] > d[u] + e[i].w) {
d[e[i].to] = d[u] + e[i].w;
q.push(make_pair(d[e[i].to], e[i].to));
}
}
}
printf("%d\n", d[n + 1]);
return 0;
}