https://cn.vjudge.net/problem/HDU-4289
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=1e4+9;
const int maxm=1e5+9;
struct E
{
int to,nex,cap,flow;
}edge[maxm];
int head[maxn];
int tot;
int gap[maxn],dep[maxn],cur[maxn];
void init()
{
tot=0;
mem(head,-1);
}
void addedge(int u,int v,int w,int rw=0)
{
edge[tot].to=v;
edge[tot].cap=w;
edge[tot].flow=0;
edge[tot].nex=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].cap=rw;
edge[tot].flow=0;
edge[tot].nex=head[v];
head[v]=tot++;
}
int Q[maxn];
void bfs(int st,int ed)
{
mem(dep,-1);
mem(gap,0);
gap[0]=1;
int fr=0,re=0;
dep[ed]=0;
Q[re++]=ed;
while(fr!=re)
{
int u=Q[fr++];
for(int i=head[u];i!=-1;i=edge[i].nex)
{
int v=edge[i].to;
if(dep[v]!=-1) continue;
Q[re++]=v;
dep[v]=dep[u]+1;
gap[dep[v]]++;
}
}
}
int S[maxn];
int sap(int st,int ed,int n)
{
bfs(st,ed);
memcpy(cur,head,sizeof(head));
int top=0;
int u=st;
int ans=0;
while(dep[st]<n)
{
if(u==ed)
{
int Min=INF;
int inser;
for(int i=0;i<top;i++)
{
if(Min>edge[S[i]].cap-edge[S[i]].flow)
{
Min=edge[S[i]].cap-edge[S[i]].flow;
inser=i;
}
}
for(int i=0;i<top;i++)
{
edge[S[i]].flow+=Min;
edge[S[i]^1].flow-=Min;
}
ans+=Min;
top=inser;
u=edge[S[top]^1].to;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-1;i=edge[i].nex)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=i;
break;
}
}
if(flag)
{
S[top++]=cur[u];
u=v;
continue;
}
int Min=n;
for(int i=head[u];i!=-1;i=edge[i].nex)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=st) u=edge[S[--top]^1].to;
}
return ans;
}
int n,m,s,t;
int w[maxn];
int main()
{
FAST_IO;
//freopen("input.txt","r",stdin);
while(cin>>n>>m)
{
init();
cin>>s>>t;
for(int i=1;i<=n;i++)
{
cin>>w[i];
addedge(i,i+n,w[i]);
//addedge(i+n,i,w[i]);
}
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
addedge(u+n,v,INF);
addedge(v+n,u,INF);
}
t=t+n;
int ans=sap(s,t,2*n);
cout<<ans<<endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N=1e4+9;
const int M=4e4+9;
int head[N],tot,dist[N],cur[N];
int s,t;
int n,m;
int w[N];
struct Edge
{
int from,to,next,flow,cap;
}edge[N*10];
void addedge(int a,int b,int c)
{
edge[tot].from = a;edge[tot].to = b;edge[tot].cap = c;edge[tot].flow = 0;edge[tot].next = head[a];head[a] = tot++;
edge[tot].from = b;edge[tot].to = a;edge[tot].cap = 0;edge[tot].flow = 0;edge[tot].next = head[b];head[b] = tot++;
}
bool BFS()
{
memset(dist,0,sizeof(dist));
dist[s] = 1;
queue<int> que;
que.push(s);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i = head[u];~i;i = edge[i].next)
{
int to = edge[i].to;
if(!dist[to]&&edge[i].cap > edge[i].flow)
{
dist[to] = dist[u] + 1;
que.push(to);
}
}
}
if(!dist[t])return false;
return true;
}
int DFS(int pos,int MinFlow)
{
if(pos==t||MinFlow==0)return MinFlow;
int flow = 0;
for(int &i = cur[pos];~i;i = edge[i].next)
{
int to = edge[i].to;
if(dist[to]==dist[pos]+1&&edge[i].cap > edge[i].flow){
int res = DFS(edge[i].to,min(MinFlow,edge[i].cap-edge[i].flow));
edge[i].flow+=res;
edge[i^1].flow-=res;
flow+=res;
MinFlow-=res;
if(MinFlow==0)break;
}
}
return flow;
}
int Dinic(int st,int et)
{
if(st==et)return 0;
int MaxFlow = 0;
while(BFS())
{
memcpy(cur,head,sizeof(head));
int ans = DFS(st,INF);
MaxFlow+=ans;
}
return MaxFlow;
}
void init()
{
tot=0;
mem(head,-1);
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
while(cin>>n>>m)
{
init();
cin>>s>>t;
for(int i=1;i<=n;i++)
{
cin>>w[i];
addedge(i,i+n,w[i]);
//addedge(i+n,i,w[i]);
}
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
addedge(u+n,v,INF);
addedge(v+n,u,INF);
}
t=t+n;
int ans=Dinic(s,t);
cout<<ans<<endl;
}
return 0;
}

322

被折叠的 条评论
为什么被折叠?



