关于 SPFA
  • 板块灌水区
  • 楼主reveal
  • 当前回复5
  • 已保存回复5
  • 发布时间2021/7/20 11:58
  • 上次更新2023/11/4 14:04:56
查看原帖
关于 SPFA
523491
reveal楼主2021/7/20 11:58

@UNVRS 这个人用 SPFA 叉最短路题鲨红了眼,他现在失去了理智,请问我应该怎么阻止他?

以下他最近叉完的 List

  1. P4779【模板】单源最短路径(标准版)

  2. P5905【模板】Johnson 全源最短路

  3. P4768[NOI2018] 归程

  4. LOJ P2769「ROI 2017 Day 1」前往大都会

  5. P3008 USACO11JAN Roads and Planes G

改动了一点点的源码

//这是一份 SPFA 提交 
#include<ctime>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define reg register
#define uni unsigned
using namespace std;
const uni N=100010,W=3,WT=0,INF=0x7fffffff,SIZE=1<<24;
uni w[N],d[N<<4],map[N],a[N],b[N<<1][3],seed=time(0),OnF,st[10];
char BuF[SIZE],*InF=BuF,WuF[SIZE];
uni read(){
    reg uni x=0;
    for(;*InF<33;++InF);
    for(;32<*InF;x=x*10+(*InF++^48));
    return(x);
}
void write(reg uni x){
    if(!x) WuF[++OnF]=48;
    for(;x;x/=10) st[++st[0]]=x%10+48;
    for(;st[0];WuF[++OnF]=st[st[0]--]);
    WuF[++OnF]=' ';
}
uni rnd(uni l,uni r){
    seed^=seed<<17;seed^=seed>>15;seed^=seed<<3;
    return(seed%(r-l+1)+l);
}
void change(reg uni &x,reg uni &y){
    swap(map[x],map[y]);
    swap(x,y);
}
void spfa_PMF(uni s){
    memset(w,127,sizeof(w));
    for(reg uni h=w[d[0]=s]=0,t=0;h<=t;++h){
        if(h+W+W+3<t)
            for(reg uni i=0,l=h+W+1,r=t-W-1;i<=WT;++i){
                reg uni x=rnd(l,r);
                if(w[d[h]]>w[d[x]]) change(d[h],d[x]);
            }
        reg uni &hd=d[h];
        for(reg uni i=0,*zh=d+h+1,*tl=d+t;i<W&&zh<tl;++i,++zh,--tl){
            if(w[hd]>w[*zh]) change(hd,*zh);
            if(w[hd]>w[*tl]) change(hd,*tl);
        }
        for(reg uni i=a[hd];i;i=b[i][0]){
            reg uni nxt=b[i][1],p=w[hd]+b[i][2];
            if(w[nxt]>p){
                w[nxt]=p;
                if(!map[nxt]){
                    d[map[nxt]=++t]=nxt;
                    if(t>h+1&&w[d[t-1]]<w[d[t]]) change(d[t],d[t-1]);
                }else if(w[nxt]<w[d[t]]) change(d[map[nxt]],d[t]);
            }
        }
        map[hd]=0;
    }
}
int main(){
    fread(BuF,1,SIZE,stdin);
    reg uni n=read(),m=read(),s=read();
    for(reg uni last=0,x;m;--m){
        b[++last][0]=a[x=read()];
        b[a[x]=last][1]=read();
        b[last][2]=read();
    }
    spfa_PMF(s);
    for(reg uni *i=w,*r=w+n;i<r;write(*++i));
    fwrite(WuF+1,1,OnF,stdout);
    fclose(stdin);
    fclose(stdout);
    return(0);
}
2021/7/20 11:58
加载中...