BZOJ4070: [Apio2015]雅加达的摩天楼

来源:互联网 发布:mysql数据库出现乱码 编辑:程序博客网 时间:2024/04/27 19:09

暴力建图大概谁都会吧(虽然我一开始建错了[捂脸熊]),所以还是说一下。
从每个起点枚举走的步数,连对应长度的边,跑dij即可(为什么那么多人自信spfa?)
这样最差情况下是n^2条边的…肯定跑不过所有的数据。
注意到当步长>sqrt(n)的时候,建出的边只有sqrt(n)条,所以步长>sqrt(n)的时候解决了,那么步长 < sqrt(n)呢?
也就是说每个点向外的步长 < sqrt(n)的边数不会超过sqrt(n)条,很多边都是重合的,只需要将它们合并即可。
总复杂度(n* sqrt(n) *logn)
对了,0x3f3f3f3f少打一个f可是会身败名裂的…

#include<queue>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>//by:MirrorGrayusing namespace std;const int N=31111,M=12111111,inf=0x3f3f3f3f;int dis[N],fz[N];int tot=-1,head[N],ver[M],nxt[M],len[M];struct doge{    int b,p,po;    bool operator <(const doge&c)const{        if(p!=c.p)return p<c.p;        if(po!=c.po)return po<c.po;        return b<c.p;    }}p[N];struct node{    int x,dis;    node(int a=0,int b=0){        x=a;dis=b;    }    bool operator <(const node&b)const{        return dis>b.dis;    }};priority_queue <node> q;void add(int x,int y,int z){    nxt[++tot]=head[x];head[x]=tot;ver[tot]=y;len[tot]=z;}int dij(int x,int y){    static bool vis[N];    memset(dis,0x3f,sizeof(dis));    memset(vis,false,sizeof(vis));    q.push(node(x,0));dis[x]=0;    while(!q.empty()){        int x=q.top().x;q.pop();        if(vis[x])continue;        vis[x]=true;        for(int i=head[x];~i;i=nxt[i])        if(dis[ver[i]]>dis[x]+len[i]){            dis[ver[i]]=dis[x]+len[i];            q.push(node(ver[i],dis[ver[i]]));        }    }    return dis[y]==inf?-1:dis[y];//身败名裂… }int main(){    memset(head,-1,sizeof(head));    int n,m;scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++){        scanf("%d%d",&p[i].b,&p[i].p);        p[i].po=p[i].b%p[i].p;    }    int bi=p[1].b,en=p[2].b;    sort(p+1,p+1+m);    for(int i=1,j=1;i<=m;){        while(j<=m && p[i].po==p[j].po && p[i].p==p[j].p)j++;        int tmp=p[i].p,top=0;        while(i<j)fz[++top]=p[i].b,i++;        sort(fz+1,fz+1+top);fz[0]=-inf,fz[top+1]=inf;        for(int sh=1;sh<=top;sh++)        for(int k=fz[sh]-tmp;k>=0 && k>=fz[sh-1];k-=tmp)add(fz[sh],k,(fz[sh]-k)/tmp);        for(int sh=top;sh;sh--)        for(int k=fz[sh]+tmp;k<n && k<=fz[sh+1];k+=tmp)add(fz[sh],k,(k-fz[sh])/tmp);    }    printf("%d\n",dij(bi,en));    return 0;}
0 0
原创粉丝点击