[BZOJ1497][NOI2006]最大获利(最小割)
2017-01-30 11:09
393 查看
题目描述
传送门题解
对于每一个中转站,s->i,pi,割掉表示花费pi建立中转站对于每一个顾客,i->t,ci,割掉表示放弃ci的收益
如果一个顾客x需要某一个中转站y,那么y->x,inf,因为py和cx不能同时满足,必须选一条割掉
所有顾客的收益之和减去最小割即为答案
代码
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<queue> using namespace std; #define N 100005 #define inf 1000000000 int n,m,p,a,b,c,s,t,sum,maxflow; int tot,point ,nxt[N*4],v[N*4],remain[N*4]; int deep ,cur ,last ,num ; queue <int> q; void addedge(int x,int y,int cap) { ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; } void bfs(int t) { for (int i=s;i<=t;++i) deep[i]=t; deep[t]=0; for (int i=s;i<=t;++i) cur[i]=point[i]; while (!q.empty()) q.pop(); q.push(t); while (!q.empty()) { int now=q.front();q.pop(); for (int i=point[now];i!=-1;i=nxt[i]) if (deep[v[i]]==t&&remain[i^1]) { deep[v[i]]=deep[now]+1; q.push(v[i]); } } } int addflow(int s,int t) { int now=t,ans=inf; while (now!=s) { ans=min(ans,remain[last[now]]); now=v[last[now]^1]; } now=t; while (now!=s) { remain[last[now]]-=ans; remain[last[now]^1]+=ans; now=v[last[now]^1]; } return ans; } void isap(int s,int t) { bfs(t); for (int i=s;i<=t;++i) ++num[deep[i]]; int now=s; while (deep[s]<t) { if (now==t) { maxflow+=addflow(s,t); now=s; } bool has_find=false; for (int i=cur[now];i!=-1;i=nxt[i]) if (deep[v[i]]+1==deep[now]&&remain[i]) { has_find=true; cur[now]=i; last[v[i]]=i; now=v[i]; break; } if (!has_find) { int minn=t-1; for (int i=point[now];i!=-1;i=nxt[i]) if (remain[i]) minn=min(minn,deep[v[i]]); if (!(--num[deep[now]])) break; ++num[deep[now]=minn+1]; cur[now]=point[now]; if (now!=s) now=v[last[now]^1]; } } } int main() { tot=-1;memset(point,-1,sizeof(point)); scanf("%d%d",&n,&m); s=1,t=n+m+2; for (int i=1;i<=n;++i) { scanf("%d",&p); addedge(s,1+i,p); } for (int i=1;i<=m;++i) { scanf("%d%d%d",&a,&b,&c); addedge(1+n+i,t,c); addedge(1+a,1+n+i,inf); addedge(1+b,1+n+i,inf); sum+=c; } isap(s,t); printf("%d\n",sum-maxflow); }
相关文章推荐
- BZOJ 1497 [NOI2006]最大获利(最小割)
- BZOJ 1497 [NOI2006]最大获利 (最小割)
- Bzoj 1497: [NOI2006]最大获利(最小割)
- bzoj 1497 [NOI2006]最大获利【最大权闭合子图+最小割】
- [NOI2006]最大获利 BZOJ1497 - 最小割
- bzoj 1497: [NOI2006]最大获利 -- 最小割
- [最小割]BZOJ 1497——[NOI2006]最大获利
- [bzoj1497][NOI2006]最大获利_网络流_最小割
- BZOJ 1497: [NOI2006]最大获利 最小割
- bzoj 1497: [NOI2006]最大获利 最小割 最大权闭和子图
- 【BZOJ】1497: [NOI2006]最大获利 最大权闭合子图或最小割
- bzoj 1497: [NOI2006]最大获利 最小割
- 【BZOJ1497】【NOI2006】最大获利
- BZOJ 1497[NOI 2006] 最大获利
- BZOJ 1497 [NOI2006] 最大获利
- BZOJ 1497: [NOI2006]最大获利
- [BZOJ1497][NOI2006]最大获利(最大权闭合子图)
- BZOJ 1497 [NOI2006]最大获利
- BZOJ 1497 [NOI2006]最大获利 最大权闭合子图
- bzoj1497【NOI2006】最大获利