spfa的复活

近年来,菊花图,stl,dij优化等的兴起,使spfa前途堪忧。

在不久前,又得知一个可悲的消息:

关于spfa-------他死了!

高兴的是:

spfa---他复活了!

方法:把spfa的队列换成优先队列。

完了。

#include<bits/stdc++.h>
#pragma GCC optimize(3)
namespace ZDY{
    #define res register
    #define ri res int
    #define ll long long
    #define db double
    #define il inline
    #define MB template <class T>
    #define Fur(i,x,y) for(ri i=x;i<=y;i++)
    #define fur(i,x,y) for(i=x;i<=y;i++)
    #define Fdr(i,x,y) for(ri i=x;i>=y;i--)
    #define in2(x,y) in(x),in(y)
    #define in3(x,y,z) in2(x,y),in(z)
    #define in4(a,b,c,d) in2(a,b);in2(c,d)
    #define outn(x) out(x),pc('
')
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fl(i,x) for(ri i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define inf 2147483647
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdin)
    #define gt io.gc()
    MB il T ABS(T x){return x>0?x:-x;}
    MB il T MAX(T x,T y){return x>y?x:y;}
    MB il T MIN(T x,T y){return x<y?x:y;}
    MB il T GCD(T x,T y){return y?GCD(y,x%y):x;}
    MB il void SWAP(T x,T y){T t=x;y=t;x=y;}
}using namespace ZDY;using namespace std;

class IO{
   #define fok (ch!=EOF)
   #define sep (ch==' '||ch=='
'||ch=='	')
   #define dsep !isdigit(ch)
   #define neq(a,b) ((a)-(b)>1e-6)
   char rbuf[1000002],wbuf[1000002],b,*p1,*p2;
   int rs,ws,S;
   public:
       IO():p1(rbuf),p2(wbuf),S(1000000),rs(1000000),ws(-1),b(1){}
       ~IO(){fwrite(wbuf,1,ws+1,stdout);}
       il char gc(){return rs==S&&(p1=rbuf,rs=-1,(S=fread(rbuf,1,S+1,stdin)-1)==-1)?(b=0,EOF):(++rs,*p1++);}
       il void pc(int x){ws==1000000&&(p2=wbuf,ws=-1,fwrite(wbuf,1,1000001,stdout)),++ws,*p2++=x;}
       il void puts(const char str[]){fwrite(wbuf,1,ws+1,stdout)?(ws=-1):0,fwrite(str,1,strlen(str),stdout);}
       il void gl(string& s){for(res char ch;(ch=gc())!='
'&&fok;)s+=ch;}
       il IO& operator>>(int& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(ll& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(char& ch){return ch=gc(),*this;}
       il IO& operator>>(string& s){res char ch=gc();while(sep&&fok)ch=gc();while(!sep&&fok)s+=ch,ch=gc();return *this;}
       il IO& operator>>(double& x){x=0;res char f=0,ch=gc();double d=0.1;while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=x*10+(ch^48),ch=gc();if(ch=='.')while(isdigit(ch=gc()))x+=d*(ch^48),d*=0.1;return x=f?-x:x,*this;}
       il IO& operator<<(int x){res char ch[10],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(ll x){res char ch[20],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(char ch){return pc(ch),*this;}
       il IO& operator<<(char str[]){return puts(str),*this;}
       il IO& operator<<(double x){int y=int(x);*this<<y;x-=y;while(neq(x,int(x)))x*=10;x?*this<<'.'<<int(x):0;return *this;}
       il operator bool(){return b;}
}io;

#define N 100001
int n,m,d[N],s,head[N];
bool v[N];
struct cmp{bool operator()(int a,int b){return d[a]>d[b];}};
struct edge{int to,w,nxt;}e[N*2];
priority_queue<int,vector<int>,cmp>q;
il void spfa(){
    clr(d,126);
    d[s]=0;q.push(s);
    int x;
    while(!q.empty()){
        v[x=q.top()]=0;q.pop();
        fl(i,x)
        if(d[x]+e[i].w<d[to]){
            d[to]=d[x]+e[i].w;
            if(!v[to])q.push(to),v[to]=1;
        }
    }
}
int main(){
    io>>n>>m>>s;
    int x;
    Fur(i,1,m)io>>x>>e[i].to>>e[i].w,e[i].nxt=head[x],head[x]=i;
    spfa();
    Fur(i,1,n)io<<d[i]<<' ';
}
View Code
原文地址:https://www.cnblogs.com/mimiorz/p/9734984.html