题意:给一个无向图,每条边上都有容量的限制,要求求出给定起点和终点的最大流。
思路:每条无向边就得拆成2条,每条还得有反向边,所以共4条。源点汇点已经给出,所以不用建了。直接在图上跑最大流就可以了。
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
const int mod=1e9+;
int s, t; int path[N], flow[N];
vector<int> vect[N]; struct node
{
int from, to, cap, flow;
node(){};
node(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){};
}edge[];
int edge_cnt; void add_node(int from,int to,int cap,int flow)
{
edge[edge_cnt]=node(from,to,cap,flow);
vect[from].push_back(edge_cnt++);
} int BFS(int s,int e)
{
deque<int> que(,s);
flow[s]=INF;
while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(!flow[e.to] && e.cap>e.flow)
{
flow[e.to]=min(flow[e.from],e.cap-e.flow);
path[e.to]=vect[x][i];
que.push_back(e.to);
}
}
if(flow[e]) return flow[e];
}
return flow[e];
} int max_flow(int s,int e)
{
int ans_flow=;
while(true)
{
memset(path,,sizeof(path));
memset(flow,,sizeof(flow)); int tmp=BFS(s,e);
if(!tmp) return ans_flow;
ans_flow+=tmp; int ed=e;
while(ed!=s)
{
int t=path[ed];
edge[t].flow+=tmp;
edge[t^].flow-=tmp;
ed=edge[t].from;
}
}
}
int main()
{
freopen("input.txt", "r", stdin);
int n, a, b, v, c, j=;
while(scanf("%d",&n),n)
{
edge_cnt=;
memset(edge,,sizeof(edge));
for(int i=; i<=n+; i++) vect[i].clear(); scanf("%d%d%d", &s, &t, &c);
for(int i=; i<c; i++)
{
scanf("%d%d%d",&a,&b,&v);
add_node(a, b, v, );
add_node(b, a, , );
add_node(b, a, v, );
add_node(a, b, , );
}
printf("Network %d\n",++j);
printf("The bandwidth is %d.\n\n", max_flow(s ,t) );
}
return ;
}
AC代码