各类模板

Author Avatar
ZC 3月 04, 2019
  • 在其它设备中阅读本文章

网络流:

int n,m,st,ed,cnt=1;
int head[N],d[N],q[N];
struct edge{int to,nxt,w;}e[N*N];
void add(int x,int y,int w){
    e[++cnt].to=y;e[cnt].w=w;e[cnt].nxt=head[x];head[x]=cnt;
    e[++cnt].to=x;e[cnt].w=0;e[cnt].nxt=head[y];head[y]=cnt;
}
bool bfs(){
    int h=0,t=1,x;
    clr(d,0);d[q[h]=st]=1;
    while(h<t){
        x=q[h++];if(x==ed)return 1;
        fl(i,x)if(e[i].w&&!d[to]){
            d[to]=d[x]+1;
            q[t++]=to;
        }
    }
    return 0;
}
int dfs(int x,int mf){
    if(x==ed)return mf;
    int us=0,w;
    fl(i,x)
    if(e[i].w&&d[to]==d[x]+1){
        w=dfs(to,MIN(mf-us,e[i].w));
        e[i].w-=w;e[i^1].w+=w;
        us+=w;
        if(us==mf)return mf;
    }
    if(!us)d[x]=-1;
    return us;
}

最大费用最大流:

int n,m,st,ed,cnt=1,mxc=0,mxf=0;
int head[N],d[N],pre[N],num[N],mf[N],ans[N];
bool v[N];
struct cmp{bool operator()(int x,int y){return d[x]<d[y];}};
priority_queue<int,vector<int>,cmp>q;
struct edge{int to,nxt,w,c;}e[500005];
void add(int x,int y,int w,int c){
    e[++cnt].to=y;e[cnt].w=w;e[cnt].c=c;e[cnt].nxt=head[x];head[x]=cnt;
    e[++cnt].to=x;e[cnt].w=0;e[cnt].c=-c;e[cnt].nxt=head[y];head[y]=cnt;
}
bool spfa(){
    clr(d,0xef);
    d[st]=0;mf[st]=inf;q.push(st);
    pre[ed]=0;
    while(!q.empty()){
        int x=q.top();q.pop();v[x]=0;
        fl(i,x)if(e[i].w&&d[x]+e[i].c>d[to]){
            d[to]=d[x]+e[i].c;
            pre[to]=x;num[to]=i;mf[to]=MIN(mf[x],e[i].w);
            if(!v[to])v[to]=1,q.push(to);
        }
    }
    return pre[ed]!=0;
}
void dinic(){
    while(spfa()){
        mxf+=mf[ed];mxc+=mf[ed]*d[ed];
        for(int x=ed;x&&x!=st;x=pre[x]){
            e[num[x]].w-=mf[ed];
            e[num[x]^1].w+=mf[ed];
        }
    }
}

二分图:

dinic:飞行员配对方案问题

#include<cstdio>
#include<cstring>
#define il inline
#define rg register
#define Fur(i,x,y) for(rg int i=x;i<=y;i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define inf 2000000000
#define fl(i,x) for(rg int i=head[x],to;to=e[i].to,i;i=e[i].nxt)
int MIN(int x,int y){return x<y?x:y;}
using namespace std;
namespace IO{const char *ln="\n";bool EOF_FLG=0;struct Reader{char buf[1<<20],*s,*t;Reader():s(buf),t(buf){};il char gt(){return s==t&&((t=(s=buf)+fread(buf,1,1<<20,stdin))==s)?EOF:(*s++);}Reader &operator>>(char *str){if(EOF_FLG)return *str=0,*this;while((*str=gt())!=' '&&*str!='\n'&&*str!=EOF)++str;if(*str==EOF)EOF_FLG=true;*str=0;return *this;}template<typename T>Reader &operator>>(T &x){if(EOF_FLG)return *this;rg char c=0,d;while(c !=EOF&&(c<'0' || c>'9'))d=c,c=gt();if(c==EOF){EOF_FLG=true;return *this;}else x=0;while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=gt();if(d=='-')x=-x;return *this;}}in;struct Writer{char buf[1<<20],*s,*t;Writer():s(buf),t(buf+(1<<20)){}~Writer(){ fwrite(buf,1,s-buf,stdout);}il void pt(char c){(s==t)?(fwrite(s=buf,1,1<<20,stdout),*s++=c):(*s++=c);}template<typename T>Writer &operator<<(T x){if(!x)return pt('0'),*this;if(x<0)pt('-'),x=-x;rg char a[40],t=0;while(x)a[t++]=x%10,x/=10;while(t--)pt(a[t]+'0');return *this;}Writer &operator<<(const char *s){while(*s)pt(*s++);return *this;}}out;}using namespace IO;
#define N 111
int n,m,st,ed,cnt=1;
int head[N],d[N],q[N];
struct edge{int to,nxt,w;}e[N*N];
void add(int x,int y,int w){
    e[++cnt].to=y;e[cnt].w=w;e[cnt].nxt=head[x];head[x]=cnt;
    e[++cnt].to=x;e[cnt].w=0;e[cnt].nxt=head[y];head[y]=cnt;
}
bool bfs(){
    int h=0,t=1,x;
    clr(d,0);d[q[h]=st]=1;
    while(h<t){
        x=q[h++];if(x==ed)return 1;
        fl(i,x)if(e[i].w&&!d[to]){
            d[to]=d[x]+1;
            q[t++]=to;
        }
    }
    return 0;
}
int dfs(int x,int mf){
    if(x==ed)return mf;
    int us=0,w;
    fl(i,x)
    if(e[i].w&&d[to]==d[x]+1){
        w=dfs(to,MIN(mf-us,e[i].w));
        e[i].w-=w;e[i^1].w+=w;
        us+=w;
        if(us==mf)return mf;
    }
    if(!us)d[x]=-1;
    return us;
}
int main(){
    in>>m>>n;
    st=n+m+1;ed=st+1;
    int x,y,ans=0;
    Fur(i,1,m)add(st,i,1);
    Fur(i,m+1,n)add(i,ed,1);
    while(1){
        in>>x>>y;
        if(x==-1&&y==-1)break;
        add(x,y,m+10);
    }
    while(bfs())ans+=dfs(st,inf);
    if(!ans)return out<<"No Solution!\n",0;
    out<<ans<<ln;
    Fur(i,n+n+2,cnt){
        if(e[i^1].w)out<<e[i^1].to<<" "<<e[i].to<<ln;
        i++;
    }
}

匈牙利:

#define N 1011
bool v[N][N],u[N];
int p[N],n,m,e,s=0;
inline bool find(int x){
    Fur(i,1,n)
    if(v[x][i]&&!u[i]){
        u[i]=1;
        if(!p[i]||find(p[i])){p[i]=x;return 1;}
    }return 0;
}
int main(){
    int x,y;
    in>>n>>m>>e;
    while(e--){in>>x>>y;if(y<=m&&x<=m)v[x][y]=1;}
    Fur(i,1,n){clr(u,0);if(find(i))s++;}
    out<<s<<ln;
}

spfa:

最小费用最大流:(dinic思想+spfa)

【模板】最小费用最大流

#include<bits/stdc++.h>
namespace ZDY{
    #pragma GCC optimize(3)
    #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
    #pragma GCC target("sse,sse2,avx,avx2,popcnt,tune=native")
    #define il __inline__ __attribute__ ((always_inline))
    #define rg register
    #define ll long long
    #define db double
    #define sht short
    #define MB template <class T>
    #define Fur(i,x,y) for(rg int i=x;i<=y;i++)
    #define Fdr(i,x,y) for(rg int i=x;i>=y;i--)
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdout)
    #define l2(n) (int(log2(n)))
    #define inf 0x3f3f3f3f
    #define fl(i,x) for(rg int i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define fcin ios::sync_with_stdio(false)
    MB T ABS(T x){return x>0?x:-x;}
    MB T MAX(T x,T y){return x>=y?x:y;}
    MB T MIN(T x,T y){return x<=y?x:y;}
    MB T GCD(T x,T y){return y?GCD(y,x%y):x;}
    void SWAP(int &x,int &y){x^=y;y^=x;x^=y;}
}using namespace ZDY;using namespace std;
namespace IO{const char* ln="\n";const int str=1<<20;struct IN{char buf[str],*s,*t;bool _;IN():s(buf),t(buf),_(0){}il char gc(){return s==t&&((t=(s=buf)+fread(buf,1,str,stdin))==s)?EOF:(*s++);}IN&operator>>(char&ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)_=1;else ch=c;return *this;}IN& operator>>(char* ch){if(_)return *this;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)return _=1,*this;*ch=c;ch++;while((c=gc())!=EOF&&!isspace(c))*ch=c,ch++;if(c==EOF)_=1;return *this;}template<typename T>IN&operator>>(T&x){if(_)return *this;char c=gc();bool ff=0;while(c!=EOF&&(c<'0'||c>'9'))ff^=(c=='-'),c=gc();if(c==EOF){_=1;return *this;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-48,c=gc();if(c==EOF)_=1;if(ff)x=-x;return *this;}}in;struct OUT{char buf[str],*s,*t;OUT():s(buf),t(buf+str){}~OUT(){fwrite(buf,1,s-buf,stdout);}void pt(char c){(s==t)?(fwrite(s=buf,1,str,stdout),*s++=c):(*s++=c);}OUT&operator<<(const char*s){while(*s)pt(*s++);return *this;}OUT&operator<<(char*s){while(*s)pt(*s++);return *this;}template<typename T>OUT&operator<<(T x){if(!x)return pt('0'),*this;if(x<0)pt('-'),x=-x;char a[30],t=0;while(x)a[t++]=x%10,x/=10;while(t--)pt(a[t]+'0');return *this;}}out;}using namespace IO;
#define N 5011
int n,m,st,ed,cnt=1;
int head[N],d[N],pre[N],num[N],mf[N];
bool v[N];
struct cmp{bool operator()(int x,int y){return d[x]>d[y];}};
priority_queue<int,vector<int>,cmp>q;
struct edge{int to,nxt,w,c;}e[100011];
void add(int x,int y,int w,int c){
    e[++cnt].to=y;e[cnt].w=w;e[cnt].c=c;e[cnt].nxt=head[x];head[x]=cnt;
    e[++cnt].to=x;e[cnt].w=0;e[cnt].c=-c;e[cnt].nxt=head[y];head[y]=cnt;
}
bool spfa(){
    clr(d,0x3f);clr(mf,0x7f);
    d[st]=0;q.push(st);
    pre[ed]=0;
    while(!q.empty()){
        int x=q.top();q.pop();v[x]=0;
        fl(i,x)if(e[i].w&&d[x]+e[i].c<d[to]){
            d[to]=d[x]+e[i].c;
            pre[to]=x;num[to]=i;mf[to]=MIN(mf[x],e[i].w);
            if(!v[to])v[to]=1,q.push(to);
        }
    }
    return pre[ed]!=0;
}
int main(){
    in>>n>>m>>st>>ed;
    Fur(i,1,m){
        int x,y,w,c;
        in>>x>>y>>w>>c;
        add(x,y,w,c);
    }
    int ansc=0,ansf=0;
    while(spfa()){
        ansf+=mf[ed];
        ansc+=mf[ed]*d[ed];
        int x=ed;
        while(x!=st&&x){
            e[num[x]].w-=mf[ed];
            e[num[x]^1].w+=mf[ed];
            x=pre[x];
        }
    }
    out<<ansf<<" "<<ansc<<ln;
}

线段树:

线段树套线段树:

主席树:

树链剖分:

Kmp:

AC自动机:

Fhq:

Treap:

ExGcd:

CRT:

ExCRT:

欧拉筛:

欧拉函数筛:

乘法逆元: