bzoj 2259: [Oibh]新型计算机 最短路 建模

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 3000005
int head[maxn], to[maxn], nex[maxn], val[maxn],edges,s,t,pre[maxn],nxt[maxn];
struct Node{
    int dist,u;
    Node(int dist=0,int u=0):dist(dist),u(u){}
    bool operator <(Node a) const{
        return a.dist<dist;
    }
};
priority_queue<Node>Q;
void addedge(int u,int v,int c){
    nex[++edges]=head[u],head[u]=edges,to[edges]=v,val[edges]=c;
}
int d[maxn];
bool done[maxn];
void dijkstra(){
    memset(d,0x3f,sizeof(d));
    d[s]=0;
    Q.push(Node(0,s));
    while(!Q.empty()){
        int u=Q.top().u; Q.pop();
        if(done[u]) continue;
        if(u==t) break;
        done[u]=true;
        for(int v=head[u];v;v=nex[v])
            if(d[u]+val[v] < d[to[v]]){
                d[to[v]]=d[u]+val[v];
                Q.push(Node(d[to[v]],to[v]));
            }
    }
}
int main(){
    //freopen("input.in","r",stdin);
    int n,a;
    scanf("%d",&n);
    s=1, t=n+1;
    for(int i=1;i<=n;++i){
        scanf("%d",&a);
        for(int j=i+1;j<=min(n+1,a+i+1)&&!pre[j];++j) addedge(j,j-1,1), pre[j]=1;
        for(int j=i+a+1;j<=n&&!nxt[j];++j) addedge(j,j+1,1),nxt[j]=1;
        if(i+a<=n) addedge(i,i+a+1,0);
        else addedge(i,n+1,i+a-n);
    }
    dijkstra();
    printf("%d",d[t]);
    return 0;
}

  

posted @ 2018-11-07 15:04  EM-LGH  阅读(168)  评论(0编辑  收藏  举报