刚开始读错题了,把题目想简单了,看了一下Popoqqq大爷的题解。
最小割
假设全部雇佣
i向汇点T连一条容量为ai的边
对于利润(i,j)
源点S向i和j分别连一条容量为eij的边
i和j之间连一条容量为2eij的边
答案为e的和-ans
如果雇佣两个,则不用割掉
如果只雇佣一个,则要割掉2eij
如果两个都不雇佣,则割掉2eij
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> #define maxn 1010 #define maxm 4000100 #define inf 1000000000 using namespace std; int head[maxn],to[maxm],c[maxm],next[maxm],q[maxn],d[maxn]; int e[maxn][maxn]; int n,m,num,s,t,ans; void addedge(int x,int y,int z) { num++;to[num]=y;c[num]=z;next[num]=head[x];head[x]=num; num++;to[num]=x;c[num]=0;next[num]=head[y];head[y]=num; } bool bfs() { memset(d,-1,sizeof(d)); int l=0,r=1; q[1]=s;d[s]=0; while (l<r) { int x=q[++l]; for (int p=head[x];p;p=next[p]) if (c[p] && d[to[p]]==-1) { d[to[p]]=d[x]+1; q[++r]=to[p]; } } if (d[t]==-1) return 0; else return 1; } int find(int x,int low) { if (x==t || low==0) return low; int totflow=0; for (int p=head[x];p;p=next[p]) if (c[p] && d[to[p]]==d[x]+1) { int a=find(to[p],min(low,c[p])); c[p]-=a;c[p^1]+=a; low-=a;totflow+=a; if (low==0) return totflow; } if (low) d[x]=-1; return totflow; } int main() { scanf("%d",&n); num=1;s=0;t=n+1; for (int i=1;i<=n;i++) { int x; scanf("%d",&x); addedge(i,t,x); } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { scanf("%d",&e[i][j]); ans+=e[i][j]; } for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) addedge(s,i,e[i][j]),addedge(s,j,e[i][j]),addedge(i,j,2*e[i][j]),addedge(j,i,2*e[i][j]); while (bfs()) ans-=find(s,inf); printf("%d\n",ans); return 0; }