链接:https://cn.vjudge.net/problem/HDU-6214
题意:T组样例,下一行给出n、m(n个点,m条边),加下来给出源点s、汇点t,加下来m行描述边u、v(边的两端点),w边的容量。求最小割的边数。
思路:给容量乘以一个较大的系数(W)+1(即容量变为w*W+1),跑最大流,得出的最大流%W就是答案。写过一个一样的题,写这个纯属为了锻炼写网络流。
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 2200;
const int W = 300;
const int inf = 0x3f3f3f3f;
struct node
{
int to,ca,next;
}g[M];
int head[N],cur[N],deep[N],cnt,n,m,s,t;
void Init()
{
cnt=0;
memset(head,-1,sizeof(head));
return ;
}
void add(int u,int v,int w)
{
g[cnt].to=v;
g[cnt].ca=w;
g[cnt].next=head[u];
head[u]=cnt++;
return ;
}
bool bfs()
{
memset(deep,0,sizeof(deep));
queue<int> q;
int u,v;
deep[s]=1;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
//cout<<u<<endl;
if(u==t) return 1;
for(int i=head[u];i!=-1;i=g[i].next)
{
v=g[i].to;
if(g[i].ca>0&&!deep[v])
{
deep[v]=deep[u]+1;
q.push(v);
}
}
}
return deep[t]!=0;
}
int dfs(int u,int flow)
{
if(u==t||!flow) return flow;
int ans=0,nowflow,v;
for(int& i=cur[u];i!=-1;i=g[i].next)
{
v=g[i].to;
//cout<<u<<" "<<v<<endl;
if(g[i].ca>0&&deep[v]==deep[u]+1)
{
nowflow=dfs(v,min(flow,g[i].ca));
if(nowflow)
{
flow-=nowflow;
g[i].ca-=nowflow;
g[i^1].ca+=nowflow;
//return nowflow;
ans+=nowflow;
if(!flow) break;
}
}
}
if(!ans) deep[u]=0;
return ans;
//return 0;
}
int Dinic()
{
int maxflow=0,flow;
while(bfs())
{
for(int i=1;i<=n;i++)
cur[i]=head[i];
while(flow=dfs(s,inf))
maxflow+=flow;
}
return maxflow;
}
int main(void)
{
int T,u,v,w;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&s,&t);
Init();
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w*W+1);
add(v,u,0);
}
printf("%d\n",Dinic()%W);
}
return 0;
}