# 2234. 多源汇最大流()-其他

## 2234. 多源汇最大流()

### 数据范围

\(2 \le n \le 10000\),
\(1 \le m \le 10^5\),
\(0 \le c \le 10000\),

### 输入样例：

``````4 5 2 2
2 4
1 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40
``````

### 输出样例：

``````70
``````

### 解题思路

• 时间复杂度：\(O(n^2m)\)

### 代码

``````// Problem: 多源汇最大流
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2236/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=10005,M=(N+100005)*2,inf=1e9;
int n,m;
int h[N],e[M],ne[M],f[M],idx;
int S,T,s,t,sc,tc;
int q[N],hh,tt,cur[N],d[N];
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
bool bfs()
{
memset(d,-1,sizeof d);
d[S]=hh=tt=0;
q[0]=S;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)return true;
q[++tt]=y;
}
}
}
return false;
}
int dfs(int x,int limit)
{
if(x==T)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d%d%d",&n,&m,&sc,&tc);
S=0,T=n+1;
while(sc--)
{
scanf("%d",&s);
}
while(tc--)
{
scanf("%d",&t);
}
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
}
printf("%d",dinic());
return 0;
}
``````
————————

### 数据范围

\(2 \le n \le 10000\),
\(1 \le m \le 10^5\),
\(0 \le c \le 10000\),

### 输入样例：

``````4 5 2 2
2 4
1 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40
``````

### 输出样例：

``````70
``````

### 解题思路

• 时间复杂度：\(O(n^2m)\)

### 代码

``````// Problem: 多源汇最大流
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2236/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=10005,M=(N+100005)*2,inf=1e9;
int n,m;
int h[N],e[M],ne[M],f[M],idx;
int S,T,s,t,sc,tc;
int q[N],hh,tt,cur[N],d[N];
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
bool bfs()
{
memset(d,-1,sizeof d);
d[S]=hh=tt=0;
q[0]=S;
cur[S]=h[S];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==T)return true;
q[++tt]=y;
}
}
}
return false;
}
int dfs(int x,int limit)
{
if(x==T)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(S,inf))res+=flow;
return res;
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d%d%d",&n,&m,&sc,&tc);
S=0,T=n+1;
while(sc--)
{
scanf("%d",&s);
}
while(tc--)
{
scanf("%d",&t);