首页 > 代码库 > BZOJ 2118 Dijkstra

BZOJ 2118 Dijkstra

思路:

经典题 不解释

找到最小的数mn

所有都是在mod mn的意义下 搞得

i->(i+a[i])%mn  边权为a[i]

 

//By SiriusRen#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int N=500000*13,inf=0x3f3f3f3f;int n,first[N],next[N],v[N],tot,minn=inf,a[15],vis[N];ll BMin,BMax,w[N],dis[N],ans;void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}struct Node{int now,dis;}st,jy;bool operator<(Node a,Node b){return a.dis>b.dis;}priority_queue<Node>pq;int main(){    memset(first,-1,sizeof(first));    memset(dis,0x3f,sizeof(dis));    scanf("%d%lld%lld",&n,&BMin,&BMax);    for(int i=1;i<=n;i++)scanf("%d",&a[i]),minn=min(minn,a[i]);    dis[0]=0;    for(int i=1;i<=n;i++)        for(int j=0;j<minn;j++)            add(j,(j+a[i])%minn,a[i]);    pq.push(st);    while(!pq.empty()){        Node t=pq.top();pq.pop();        if(vis[t.now])continue;        vis[t.now]=1;        for(int i=first[t.now];~i;i=next[i])            if(dis[v[i]]>dis[t.now]+w[i])                dis[v[i]]=dis[t.now]+w[i],                jy.now=v[i],jy.dis=dis[v[i]],pq.push(jy);    }BMin--;    for(int i=0;i<minn;i++){        if(dis[i]<=BMin)ans-=(BMin-dis[i])/minn+1;        if(dis[i]<=BMax)ans+=(BMax-dis[i])/minn+1;    }printf("%lld\n",ans);}

 

BZOJ 2118 Dijkstra