标签:ini while set return empty string mem 网络最大流 pre
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10005
#define MAXM 100005
#define INF 2147483647
struct queue {
int q[MAXN]; int head,tail;
queue() : head(1),tail(0) {}
void reset() {
head = 1; tail = 0;
}
void push(int x) {
q[++tail] = x;
}
int front() {
return q[head];
}
void pop() {
++head;
}
bool empty() {
return head>tail;
}
}q;
struct edge {
int v,cap,next;
}G[MAXM<<1];
int head[MAXN],cur[MAXN],step[MAXN];
int ans = 0,tot = -1;
int N,M,S,T;
inline bool bfs() {
q.reset(); q.push(S);
std::memset(step,-1,sizeof(step));
step[S] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i=head[u];i!=-1;i=G[i].next) {
int v = G[i].v;
if(G[i].cap>0&&step[v]==-1) {
q.push(v);
step[v] = step[u] + 1;
}
}
}
return step[T]!=-1;
}
int dfs(int u,int a) {
if(u==T) return a;
int flow = 0,temp;
for(int& i=cur[u];i!=-1;i=G[i].next) {
int v = G[i].v;
if(step[u]+1==step[v]&&G[i].cap&&(temp = dfs(v,std::min(a,G[i].cap)))>0) {
flow += temp; a -= temp;
G[i].cap -= temp; G[i^1].cap += temp;
if(a==0) break;
}
}
return flow;
}
inline void dinic() {
while(bfs()) {
for(int i=1;i<=N;++i) cur[i] = head[i];
ans += dfs(S,INF);
}
}
inline void add(int u,int v,int cap) {
G[++tot].v = v; G[tot].cap = cap; G[tot].next = head[u]; head[u] = tot;
}
int main() {
int u,v,cap;
std::memset(head,-1,sizeof(head));
scanf("%d%d%d%d",&N,&M,&S,&T);
for(int i=1;i<=M;++i) {
scanf("%d%d%d",&u,&v,&cap);
add(u,v,cap); add(v,u,0);
}
dinic();
printf("%d",ans);
return 0;
}
标签:ini while set return empty string mem 网络最大流 pre
原文地址:https://www.cnblogs.com/Neworld2002/p/10164029.html