POJ 1459 网络流

网络流模板题,这份代码转自kuangbin神,觉得还是很经得起考验的!

首先是dinic:

/*
最大流模板
dinic算法
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=150;//点数的最大值
const int MAXM=20500;//边数的最大值

struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM];
int tol;

int dep[MAXN];//dep为点的层次
int head[MAXN];

int n;
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)//第一条变下标必须为偶数
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

int BFS(int start,int end)
{
    int que[MAXN];
    int front,rear;
    front=rear=0;
    memset(dep,-1,sizeof(dep));
    que[rear++]=start;
    dep[start]=0;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>0&&dep[v]==-1)
            {
                dep[v]=dep[u]+1;
                que[rear++]=v;
                if(rear>=MAXN)rear=0;
                if(v==end)return 1;
            }
        }
    }
    return 0;
}
int dinic(int start,int end)
{
    int res=0;
    int top;
    int stack[MAXN];//stack为栈,存储当前增广路
    int cur[MAXN];//存储当前点的后继
    while(BFS(start,end))
    {
        memcpy(cur,head,sizeof(head));
        int u=start;
        top=0;
        while(1)
        {
            if(u==end)
            {
                int min=INF;
                int loc;
                for(int i=0;i<top;i++)
                  if(min>edge[stack[i]].cap)
                  {
                      min=edge[stack[i]].cap;
                      loc=i;
                  }
                for(int i=0;i<top;i++)
                {
                    edge[stack[i]].cap-=min;
                    edge[stack[i]^1].cap+=min;
                }
                res+=min;
                top=loc;
                u=edge[stack[top]].from;
            }
            for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
              if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])
                 break;
            if(cur[u]!=-1)
            {
                stack[top++]=cur[u];
                u=edge[cur[u]].to;
            }
            else
            {
                if(top==0)break;
                dep[u]=-1;
                u=edge[stack[--top]].from;
            }
        }
    }
    return res;
}

int main()//多源多汇点,在前面加个源点,后面加个汇点,转成单源单汇点
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int start,end;
    int np,nc,m;
    int u,v,z;
    while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
    {
        init();
        while(m--)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&z);
            u++;v++;
            addedge(u,v,z);
        }
        while(np--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            addedge(0,u,z);
        }
        while(nc--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            addedge(u,n+1,z);
        }
        start=0;
        end=n+1;
        int ans=dinic(start,end);
        printf("%d\n",ans);
    }
    return 0;
}

然后还喜获isap模板,同样来自kuangbin神:

/*
最大流模板
sap
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

const int MAXN=150;//点数的最大值
const int MAXM=20500;//边数的最大值
const int INF=0x3f3f3f3f;

struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM];
int tol;
int head[MAXN];
int dep[MAXN];
int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为y

int n;

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int que[MAXN];
    int front,rear;
    front=rear=0;
    dep[end]=0;
    que[rear++]=end;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap!=0||dep[v]!=-1)continue;
            que[rear++]=v;
            if(rear>=MAXN)rear=0;
            dep[v]=dep[u]+1;
            ++gap[dep[v]];
        }
    }
}
int SAP(int start,int end)
{
    int res=0;
    BFS(start,end);
    int cur[MAXN];
    int S[MAXN];
    int top=0;
    memcpy(cur,head,sizeof(head));
    int u=start;
    int i;
    while(dep[start]<n)
    {
        if(u==end)
        {
            int temp=INF;
            int inser;
            for(i=0;i<top;i++)
              if(temp>edge[S[i]].cap)
              {
                  temp=edge[S[i]].cap;
                  inser=i;
              }
            for(i=0;i<top;i++)
            {
                edge[S[i]].cap-=temp;
                edge[S[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=edge[S[top]].from;
        }
        if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路
           break;
        for(i=cur[u];i!=-1;i=edge[i].next)
           if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
             break;
        if(i!=-1)
        {
            cur[u]=i;
            S[top++]=i;
            u=edge[i].to;
        }
        else
        {
            int min=n;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(edge[i].cap==0)continue;
                if(min>dep[edge[i].to])
                {
                    min=dep[edge[i].to];
                    cur[u]=i;
                }
            }
            --gap[dep[u]];
            dep[u]=min+1;
            ++gap[dep[u]];
            if(u!=start)u=edge[S[--top]].from;
        }
    }
    return res;
}
int main()//多源多汇点,在前面加个源点,后面加个汇点,转成单源单汇点
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int start,end;
    int np,nc,m;
    int u,v,z;
    while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
    {
        init();
        while(m--)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&z);
            u++;v++;
            addedge(u,v,z);
        }
        while(np--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            addedge(0,u,z);
        }
        while(nc--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            addedge(u,n+1,z);
        }
        start=0;
        end=n+1;
        n+=2;//n一定是点的总数,这是使用SAP模板需要注意的
        int ans=SAP(start,end);
        printf("%d\n",ans);
    }
    return 0;
}

 最小费用最大流

const int maxn=500;
const int INF=0x3f3f3f3f;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int d,int p):from(u),to(v),cap(c),flow(d),cost(p) {}
};
struct MCMF
{
    int s,t,flow,cost;
    vector<Edge>edges;
    vector<int>G[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void init()
    {
        for(int i=0;i<maxn;i++)
        G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back((Edge){from,to,cap,0,cost});
        edges.push_back((Edge){to,from,0,0,-cost});
        int l=edges.size();
        G[from].push_back(l-2);
        G[to].push_back(l-1);
    }
    bool BellmanFord(int s,int t)
    {
       for(int i=0;i<maxn;i++)
        d[i]=INF;
       memset(inq,0,sizeof(inq));
       d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;
       queue<int>Q;
       Q.push(s);
       while(!Q.empty())
       {
          int u=Q.front();
          Q.pop();
          inq[u]=0;
          for(int i=0;i<G[u].size();i++)
          {
              Edge &e=edges[G[u][i]];
              if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
              {
                 d[e.to]=d[u]+e.cost;
                 p[e.to]=G[u][i];
                 a[e.to]=min(a[u],e.cap-e.flow);
                 if(!inq[e.to])
                 {
                     Q.push(e.to);
                     inq[e.to]=1;
                 }
              }
          }
       }
       if(d[t]==INF)return false;
       flow+=a[t];
       cost+=d[t]*a[t];
       int u=t;
       while(u!=s)
       {
           edges[p[u]].flow+=a[t];
           edges[p[u]^1].flow-=a[t];
           u=edges[p[u]].from;
       }
       return true;
    }
    int Mincost(int s,int t)
    {
        flow=cost=0;
        while(BellmanFord(s,t));
        return cost;
    }
}solve;

 

posted on 2016-10-09 15:19  very_czy  阅读(201)  评论(0编辑  收藏  举报

导航