百练+EK算法模版+网络流邻接表形式

点击打开链接
//在于建图
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define LL long long
#define inf 0x3f3f3f3f
#define mod 1e9+7
using namespace std;
const int maxn=405;
LL cap[maxn][maxn],flow[maxn][maxn],rest[maxn];
int snode,tnode,pre[maxn],vis[maxn];
bool BFS_Path(int m)//augmenting path
{
    memset(vis, 0, sizeof(vis));
    queue<int>Q;
    int u=0,v=0;
    u=snode; pre[u]=u;
    rest[u]=inf;vis[u]=1;
    Q.push(u);
    while (!Q.empty()) {
        u=Q.front(); Q.pop();
        for(v=0;v<=m;v++){
            if(!vis[v]&&flow[u][v]<cap[u][v]){
                pre[v]=u;vis[v]=1;
                rest[v]=min(rest[u],cap[u][v]-flow[u][v]);
                if(v==tnode) return true; // find augment path
                Q.push(v);
            }
        }
    }
    return false;
}
int max_flow(int s, int t, int m)
{
    snode=s;tnode=t;
    int ans=0;
    while (BFS_Path(m)) {
        ans+=rest[t];
        int v=t;
        while (v!=s) {
            int u=pre[v];
            flow[u][v]+=rest[t];
            flow[v][u]-=rest[t];
            v=u;
        }
    }
    return ans;
}
int main()
{
    int N=0,F=0,D=0;
    int s=0,t=0,num1=0,num2=0,num=0;
    scanf("%d %d %d",&N,&F,&D);
    memset(cap, 0, sizeof(cap));
    memset(flow, 0, sizeof(flow));
    memset(pre, 0, sizeof(pre));
    memset(rest, 0, sizeof(rest));
    s=0; t=2*N+F+D+1;
    for(int i=1;i<=F;i++) cap[s][i]=1;
    for(int i=F+2*N+1;i<=F+2*N+D;i++) cap[i][t]=1;
    for(int i=1;i<=N;i++) cap[F+2*i-1][F+2*i]=1;
    for(int i=1;i<=N;i++){
        scanf("%d %d",&num1,&num2);
        while (num1--) {
            scanf("%d",&num);
            cap[num][F+2*i-1]=1;
        }
        while (num2--) {
            scanf("%d",&num);
            cap[F+2*i][F+2*N+num]=1;
        }
    }
    printf("%d\n",max_flow(s,t,t));
    return 0;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <vector>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn=20005;
struct edge{
    int from,to,cap,flow;
    edge(int u, int v, int c, int f):from(u),to(v),cap(c),flow(f){};
};
vector<edge> ed;
vector<int> G[maxn];
int snode,tnode,pre[maxn],vis[maxn],rest[maxn];
void init(int t)
{
    for(int i=0;i<=t;i++) G[i].clear();
    ed.clear();
}
void addEdge(int from, int to, int cap)
{
    ed.push_back(edge(from, to, cap, 0));
    ed.push_back(edge(to, from, 0, 0));
    int num=ed.size();
    G[from].push_back(num-2);
    G[to].push_back(num-1);
}
bool BFS_Path(int m)///augmenting path
{
    memset(vis, 0, sizeof(vis));
    queue<int>Q;
    int u=0;
    u=snode; pre[u]=u;
    rest[u]=inf;vis[u]=1;
    Q.push(u);
    while (!Q.empty()) {
        u=Q.front(); Q.pop();
        for(int i=0;i<G[u].size();i++){
            edge &e=ed[G[u][i]];
            if(!vis[e.to]&&e.flow<e.cap){
                pre[e.to]=G[u][i];vis[e.to]=1;
                rest[e.to]=min(rest[u],e.cap-e.flow);
                if(e.to==tnode) return true; /// find augment path
                Q.push(e.to);
            }
        }
    }
    return false;///no augment path
}
int max_flow(int s, int t, int m)
{
    snode=s;tnode=t;
    int ans=0;
    while (BFS_Path(m)) {
        ans+=rest[t];
        //cout<<ans<<endl;
        /*int v=t;
        while (v!=s) {
            int u=pre[v];
            ed[u].flow+=rest[t];
            ed[v].flow-=rest[t];
            v=u;
        }
        */
        for(int e=t;e!=s;e=ed[pre[e]].from)
        {
            ed[pre[e]].flow+=rest[t];
            ed[pre[e]^1].flow-=rest[t];
        }
    }
    return ans;
}
int main()
{
    int N=0,M=0,A=0,B=0,a=0,b=0,w=0;
    memset(pre, 0, sizeof(pre));
    memset(rest, 0, sizeof(rest));
    scanf("%d %d",&N,&M);
    int s=0,t=N+1;
    init(t);
    for(int i=1;i<=N;i++){
        scanf("%d %d",&A,&B);
        addEdge(s, i, A);
        addEdge(i, t, B);
    }
    for(int i=1;i<=M;i++){
        scanf("%d %d %d",&a,&b,&w);
        ed.push_back(edge(a, b, w, 0));
        ed.push_back(edge(b, a, w, 0));
        int num=ed.size();
        G[a].push_back(num-2);
        G[b].push_back(num-1);
    }
    printf("%d\n",max_flow(s,t,t));
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值