做一遍最大流,满流设为1,其他为inf,再做一次最大流。
注意方向边流量不清零。
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 5;
const int maxs = 3000 + 5;
int n,m;
struct Edge{
int from,to,cap,flow;
};
struct Dicnic{
int n,m,s,t;
int d[maxs],cur[maxs],vis[maxs];
vector<int>G[maxs];
vector<Edge>edges;
void AddEdge(int from,int to,int cap){
Edge e1 = {from,to,cap,0};
Edge e2 = {to,from,0,0};
edges.push_back(e1);
edges.push_back(e2);
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
void init(int n){
this -> n = n;
for(int i = 0;i<=n;i++){
G[i].clear();
}
edges.clear();
}
bool BFS(){ //就是用BFS分了个层而已,一看代码便知
mem(vis,0);
queue<int>Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
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(!vis[e.to]&&e.cap>e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x ,int a){ //就是顺着层次dfs而已
if(x==t||a==0) return a;
int flow = 0 , f;
for(int &i = cur[x];i<G[x].size();i++){
Edge &e = edges[G[x][i]];
if(d[e.to]==d[x]+1&&(f = DFS(e.to,min(a,e.cap-e.flow)))>0){ //a是路径上的最小残存容量
e.flow += f;
edges[G[x][i]^1].flow -= f; //反向边
flow += f;
a -= f;
if(a==0) break; //a等于0及时退出,再从s那找一遍,因为从哪断的不知道
}
}
return flow;
}
int MaxFlow(int s,int t){
this -> s = s; this ->t = t;
int flow = 0;
while(BFS()){
mem(cur,0);
flow += DFS(s,inf);
}
return flow;
}
void build2(){
for(int i=0;i<edges.size();i+=2){
Edge &e=edges[i];
if(e.flow==e.cap){
e.cap=1;
edges[i^1].cap=0;
}
else{
e.cap=inf;
edges[i^1].cap=0;
}
e.flow=0;
//edges[i^1].flow=0; //hdu 6214中反向边不清零就过了。。。
}
}
}g;