POJ 1149 PIGS
2015-07-19 18:59
246 查看
最大流,
给定M个猪圈 N个商人 每个商人可以选择特定的猪圈买猪 当一个商人同时可以选择多个猪圈时,此时猪圈可以互通,就是此时两猪圈的流量上限为INF
给定M个猪圈 N个商人 每个商人可以选择特定的猪圈买猪 当一个商人同时可以选择多个猪圈时,此时猪圈可以互通,就是此时两猪圈的流量上限为INF
#include "cstring" #include "iostream" #include "cstdio" #include "queue" using namespace std; typedef long long LL; const int M=105; const int maxn=2147483648; const int INF = 0x3f3f3f3f; int map[M][M],flow[M][M]; int p[M],a[M]; int f,n; void EK(int s, int t) { queue<int>q; memset(flow,0,sizeof(flow)); memset(a,0,sizeof(a)); memset(p,0,sizeof(p)); f=0; while(1) { memset(a, 0, sizeof(a)); a[s] = INF; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int v = 0; v <= n+1; ++v) if(!a[v] && map[u][v] > flow[u][v]) { p[v] = u; q.push(v); a[v] = a[u] < map[u][v] - flow[u][v]? a[u]:map[u][v] - flow[u][v]; } } if(a[t] == 0) break; for(int u = t; u != s; u = p[u]) { flow[p[u]][u] += a[t]; flow[u][p[u]] -= a[t]; } f += a[t]; } } int main() { int i,j,k; int s,t,m; int num[1000+10]; int vis[1000+10]; int nn,mm; cin>>mm>>nn; s=0,t=nn+1; n=nn; memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); for(i = 1; i <= mm; ++i) cin>>num[i]; for(i = 1; i <= nn; ++i) { cin>>m; for(j = 0; j < m; ++j) { int temp; cin>>temp; if(vis[temp]==0) map[s][i] += num[temp]; // else map[vis[temp]][i]=INF; //猪圈互通 vis[temp]=i; } cin>>map[i][t]; //到超级汇点的流量 } EK(s,t); cout<<f<<endl; return 0; }
相关文章推荐
- synchronized锁分析
- Python里面如何生成随机数?
- win7 32位的4g内存可用内存只有2g到3g怎么解决?
- Linux Mint (应用软件— 下载工具:Transmission)
- 2015年07月01日第二天笔记
- 仿函数
- 1062 : 最近公共祖先·一
- R语言利用RCurl下载验证码图片
- ZOJ 3230 Solving the Problems
- 观察者模式
- 总结
- 股票学习31(公司财务分析02)
- 2015年06月30日第一天笔记
- 用Python匹配HTML tag的时候,<.*>和<.*?>有什么区别?
- [CQOI2009][BZOJ1305] dance跳舞
- maven
- Linux安装MySQL的两种方法
- 集合 list set 常用类型说明
- CAS学习笔记(二)
- LightOJ 1153 Internet Bandwidth