poj-1273(最大流)

题解:纯板子题。。。

EK算法

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 205
#define maxx 1e12
#define ll long long
using namespace std;
ll flow[maxn];//流量到达每个顶点的剩余流量;
ll c[maxn][maxn];//残余网络;
ll n,m;
ll pre[maxn];
queue<int>que;
int bfs(int s,int e)
{
    memset(pre,-1,sizeof(pre));
    while(!que.empty())
        que.pop();
    pre[s]=0;flow[s]=maxx;que.push(s);
    while(!que.empty())
    {
        int temp=que.front();
        que.pop();
        if(temp==e)
            break;
        for(int i=1;i<=n;i++)
        {
            if(i!=s&&c[temp][i]>0&&pre[i]==-1)
            {
                pre[i]=temp;
                flow[i]=min(c[temp][i],flow[temp]);
                que.push(i);
            }
        }
    }
    if(pre[e]==-1)
        return -1;
    else
        return flow[e];
}
int maxflow(int s,int e)
{
    long long int tempsum=0;
    long long int anssum=0;
    while((tempsum=bfs(s,e))!=-1)
    {
        int k=e;
        while(k!=s)
        {
            int last=pre[k];
            c[last][k]-=tempsum;
            c[k][last]+=tempsum;
            k=last;
        }
        anssum+=tempsum;
    }

    return anssum;
}
int main()
{
    int x,y,w;
    while(cin>>m>>n)
    {
        memset(c,0,sizeof(c));
        memset(flow,0,sizeof(flow));
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y>>w;
            c[x][y]+=w;
        }
        long long int  ans=maxflow(1,n);
        cout<<ans<<endl;
    }
    return 0;
}

  dinic算法

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#define maxn 5005
#define ll long long
using namespace std;
struct Edge
{
    ll next;
    ll to;
    ll w;
}edge[maxn];
ll head[maxn];
ll n,m;
ll cnt,s,e;
ll x,y,w;
ll depth[maxn];
void add(ll u,ll v,ll w)
{
    edge[cnt].next=head[u];edge[cnt].to=v;
    edge[cnt].w=w;head[u]=cnt++;
    edge[cnt].next=head[v];edge[cnt].to=u;
    edge[cnt].w=0;head[v]=cnt++;
}
bool bfs()
{
    memset(depth,0,sizeof(depth));
    queue<ll>que;
    que.push(s);
    depth[s]=1;
    while(!que.empty())
    {
        ll u=que.front();que.pop();
        for(ll i=head[u];i!=-1;i=edge[i].next)
        {
            ll v=edge[i].to;
            if(edge[i].w<=0||depth[v])
                continue;
            depth[v]=depth[u]+1;
            que.push(v);
        }
    }
    return depth[e];
}
ll dfs(ll u,ll maxflow)
{
    if(u==e)
        return maxflow;
    ll add=0;
    for(ll i=head[u];i!=-1&&add<maxflow;i=edge[i].next)
    {
        ll v=edge[i].to;
        if(depth[v]!=depth[u]+1)
            continue;
        if(edge[i].w<=0)
            continue;
        ll tempflow=dfs(v,min(maxflow-add,edge[i].w));
        edge[i].w-=tempflow;
        edge[i^1].w+=tempflow;
        add+=tempflow;
    }
    return add;
}
ll dinic()
{
    ll ans=0;
    while(bfs())
    {
        ans+=dfs(s,1e12);
    }
    return ans;
}
int main()
{
    while(cin>>m>>n)
    {
        s=1;e=n;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y>>w;
            add(x,y,w);
        }
        ll a=dinic();
    cout<<a<<endl;
    }
}

  

原文地址:https://www.cnblogs.com/huangdao/p/8987566.html