最大流问题中求解
可以当做模板直接使用
#include <iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=1000+10;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define inf 1000000;
struct edge{
int from,to,cap,flow;
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
int n,m;
vector<edge> edges;//正路和逆路
vector<int> g[maxn];//邻接表,节点i的第j条边在edges中的序号
int a[maxn];//从起点到i的可改进量
int p[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++)g[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap,int flow){
edges.push_back(edge(from,to,cap,0));
edges.push_back(edge(to,from,0,0));//一般逆路无用,设cap为0
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
int maxflow(int s,int t){
int flow=0;
for(;;){
memset(a,0,sizeof(a));
queue<int>q;
q.push(s);
a[s]=inf;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<g[x].size();i++){
edge&e=edges[g[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to]=g[x][i];
a[e.to]=max(a[e.from],e.cap-e.flow);
q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;
for(int u=t;u!=s;u=edges[p[u]].from)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
};
int main(int argc, char** argv) {
return 0;
}