BZOJ 2400 Spoj 839 Optimal Marks
来源:互联网 发布:mac应用程序安装位置 编辑:程序博客网 时间:2024/06/04 21:16
最小割
显然按位来做。我们可以发现我们实际上是在把所有点划分成两个集合,两个集合之间可能有交叉的代价,这就类似于最小割模型!要求点权和最小可以把代价放大到10000并且将所有待定点与s(表示0集合)连一条代价1的边,这样就可以有主次关系了。
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define N 505#define M 2005#define ll long longusing namespace std;struct edge{int next,to,flow;}e[M*10];const int INF = 1<<29, mod = 10000;int ecnt = 1, last[N], cur[N], n, m, k , S, T, label[N], v[N];bool d[N][N];void addedge(int a, int b, int c){ e[++ecnt]=(edge){last[a],b,c}; last[a]=ecnt; e[++ecnt]=(edge){last[b],a,0}; last[b]=ecnt;}void clr(){ S = 501; T = 502; memset(d,0,sizeof(d)); memset(v,0,sizeof(v));}void build(int pos){ ecnt=1; memset(last,0,sizeof(last)); int pre = 1<<pos; for(int i = 1; i <= n; i++) { if(v[i] == -1)addedge(S,i,1); else if(v[i] & pre)addedge(i,T,INF); else addedge(S,i,INF); for(int j = 1; j <= n; j++) if(d[i][j]) addedge(i,j,mod); }}bool bfs(){ memset(label,0,sizeof(label)); queue<int> q; q.push(S); label[S]=1; while(!q.empty()) { int x = q.front(); q.pop(); for(int i = last[x]; i; i=e[i].next) { int y=e[i].to; if(label[y] || !e[i].flow)continue; label[y] = label[x] + 1; if(y==T)return 1; q.push(y); } } return 0;}int dfs(int x, int flow){ if(x==T)return flow; int use = 0; for(int &i = cur[x]; i; i=e[i].next) { int y=e[i].to; if(label[y] != label[x] + 1)continue; int w = dfs(y, min(e[i].flow, flow-use)); e[i].flow -= w; e[i^1].flow += w; use += w; if(use == flow)return use; } return use;}int dinic(){ int ret = 0; while(bfs()) { memcpy(cur,last,sizeof(cur)); ret += dfs(S,INF); } return ret;}int main(){ clr(); scanf("%d%d",&n,&m); ll sum = 0; for(int i = 1, x; i <= n; i++) { scanf("%d",&x); if(x<0)v[i]=-1; else v[i]=x, sum+=x; } for(int i = 1, a, b; i <= m; i++) { scanf("%d%d",&a,&b); d[a][b]=d[b][a]=1; } ll eans = 0, pans = 0, ans; for(int i = 0; i < 32; i++) { build(i); ans = dinic(); eans += ans/mod * (1<<i); pans += ans%mod * (1<<i); } pans += sum; printf("%lld\n%lld\n",eans,pans);}
0 0
- bzoj 2400: Spoj 839 Optimal Marks
- BZOJ 2400 Spoj 839 Optimal Marks
- bzoj 2400: Spoj 839 Optimal Marks
- 2400: Spoj 839 Optimal Marks
- 【 bzoj 2400 】Spoj 839 Optimal Marks - 最小割
- BZOJ 2400: Spoj 839 Optimal Marks|最小割
- bzoj 2400: Spoj 839 Optimal Marks (最小割)
- [最小割] BZOJ 2400 Spoj 839 Optimal Marks
- 【BZOJ 2400】Spoj 839 Optimal Marks 最小割
- BZOJ 2400: Spoj 839 Optimal Marks 网络流
- bzoj 2400: Spoj 839 Optimal Marks(最小割)
- spoj 839(Optimal Marks)
- spoj 839 Optimal Marks
- SPOJ 839 Optimal Marks
- SPOJ 839 Optimal Marks
- 2400: Spoj 839 Optimal Marks 最小割
- BZOJ2400 Spoj 839 Optimal Marks
- BZOJ2400: Spoj 839 Optimal Marks
- Map接口与Map.Entry
- JSP剪刀石头布的设计与实现
- Opencv学习笔记-----理解Mat
- 全概率公式和贝叶斯公式
- 第八周实践三色球
- BZOJ 2400 Spoj 839 Optimal Marks
- 物理按键及onKeyDown( )方法
- iOS 证书简介
- keySet()和entrySet()
- shell编程入门(一)
- jquery操作分页导出和全部导出 复制即可使用
- ccf高速公路
- 字典
- startActivityForResult和setResult详解