Dinic(模板 再错是不可能的 这辈子都不可能了)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0);
const double eps=0.00000001;
const int N=60100;
struct node{
    int to,next;
    int flow;
}edge[N*10];
int head[N];
int dis[N];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void add(int u,int v,int flow){
    edge[tot].to=v;
    edge[tot].flow=flow;
    edge[tot].next=head[u];
    head[u]=tot++;

    edge[tot].to=u;
    edge[tot].flow=0;
    edge[tot].next=head[v];
    head[v]=tot++;
}
int BFS(int s,int t){
    queue<int>q;
    memset(dis,-1,sizeof(dis));
    q.push(s);
    dis[s]=0;
    while(q.empty()==0){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(dis[v]==-1&&edge[i].flow){
                dis[v]=dis[u]+1;
                q.push(v);
            }
        }
    }
    if(dis[t]==-1)return 0;
    return 1;
}
int DFS(int s,int t,int flow){
    if(s==t)return flow;
    int ans=0;
    for(int i=head[s];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(edge[i].flow&&dis[v]==dis[s]+1){
            int f=DFS(v,t,min(flow-ans,edge[i].flow));
            edge[i].flow=edge[i].flow-f;
            edge[i^1].flow=edge[i^1].flow+f;
            ans=ans+f;
            if(flow==ans)return flow;
        }
    }
    if(ans==0)dis[s]=-1;
    return ans;
}
int Dinc(int s,int t){
    int flow=0;
    while(BFS(s,t)){
        flow+=DFS(s,t,INF);
    }
    return flow;
}

 不再作死了

posted on 2018-04-16 21:16  见字如面  阅读(227)  评论(0编辑  收藏  举报

导航