\(Solution\)
一个较为模板的最小割,对于第二问,把图的流量变为\(1\),再跑一边网络流即可
\(Code\)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int h[50],cur[50],n,m,dep[50],q[50],tot = 1;
struct edge{
int to,nxt,z;
}e[2005];
void add(int x,int y,int z)
{
e[++tot] = edge{y,h[x],z},h[x] = tot;
e[++tot] = edge{x,h[y],0},h[y] = tot;
}
int bfs()
{
for (int i = 1; i <= n; i++) cur[i] = h[i];
memset(dep,0,sizeof dep);
int head = 0,tail = 1;
q[1] = 1,dep[1] = 1;
while (head < tail)
{
int u = q[++head];
for (int i = h[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dep[v] || e[i].z <= 0) continue;
dep[v] = dep[u] + 1,q[++tail] = v;
}
}
return dep[n];
}
int dinic(int u,int mn,int fa)
{
if (u == n || mn <= 0) return mn;
int flow = 0,sum = 0;
for (int &i = cur[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dep[v] != dep[u] + 1 || v == fa || e[i].z <= 0) continue;
sum = dinic(v,min(mn,e[i].z),u);
if (sum <= 0) continue;
flow += sum,mn -= sum,e[i].z -= sum,e[i ^ 1].z += sum;
if (mn <= 0) break;
}
return flow;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i = 1,q,p,c; i <= m; i++) scanf("%d%d%d",&q,&p,&c),add(q,p,c);
int ans = 0;
while (bfs()) ans += dinic(1,2147483647,1);
printf("%d ",ans);
for (int i = 2; i <= tot; i++)
if (i & 1) e[i].z = 0; else e[i].z = 1;
ans = 0;
while (bfs()) ans += dinic(1,2147483647,1);
printf("%d\n",ans);
}