AcWing 2280. 最优标号
很神奇的建图,但是确实很有道理
//多个进行匹配的问题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1005,M=1e6+5;
const int inf=1e9;
int h[N],ne[M],e[M],w[M],tot=1;
void add(int from,int to,int w1,int w2) {
e[++tot]=to; w[tot]=w1; ne[tot]=h[from]; h[from]=tot;
e[++tot]=from;w[tot]=w2; ne[tot]=h[to]; h[to]=tot;
}
int S=N-2,T=N-1;
int cur[N],dep[N];
bool bfs() {
memcpy(cur,h,sizeof(h));
memset(dep,0,sizeof(dep));
queue<int>q;
q.push(S);
dep[S]=1;
while(!q.empty()) {
int now=q.front();
q.pop();
for(int i=h[now];i;i=ne[i]) {
int to=e[i];
if(dep[to]==0&&w[i]>0)
dep[to]=dep[now]+1,q.push(to);
}
}
return dep[T];
}
int dfs(int now,int sum) {
if(now==T)return sum;
int ans=0;
for(int i=cur[now];i&∑i=ne[i]) {
cur[now]=i;
int to=e[i];
if(dep[to]==dep[now]+1&&w[i]>0) {
int k=dfs(to,min(sum,w[i]));
if(k==0)dep[to]=0;
w[i]-=k;
w[i^1]+=k;
sum-=k;
ans+=k;
}
}
return ans;
}
int n,m,a[N];
int x[3005],y[3005];
void build(int flag) {
memset(h,0,sizeof(h));
tot=1;
for(int i=1;i<=m;i++)add(x[i],y[i],1,1);
for(int i=1;i<=n;i++)
if(a[i]!=-1) {
if(a[i]>>flag&1)add(i,T,inf,0);
else add(S,i,inf,0);
}
}
ll dinic(int x) {
build(x);
int ans=0;
while(bfs())ans+=dfs(S,inf);
return ans;
}
int main() {
cin>>n>>m;
for(int i=1;i<=m;i++)cin>>x[i]>>y[i];
int k;cin>>k;
memset(a,-1,sizeof(a));
while(k--) {
int x,y;
cin>>x>>y;
a[x]=y;
}
ll ans=0;
for(int i=0;i<31;i++)
ans+=dinic(i)<<i;
cout<<ans<<'\n';
return 0;
}
标签:标号,dep,2280,ne,tot,int,ans,now,AcWing
From: https://www.cnblogs.com/basicecho/p/16987626.html