HDOJ 3605 Escape 解题报告 二分图匹配 dinic 网络流
来源:互联网 发布:校园网mac地址绑定错误 编辑:程序博客网 时间:2024/06/05 05:27
【Problem Description】
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
题目大意:
这道题就是给你n个人,m个星球,这n个人对m个星期有不同的意向。然而,每个星球都有自己的承受能力。我们想知道能否全部满足这n个人的意向。
【解题报告】
我们不难想出这个网络流怎么建:
源点S连接每个人,边权为1,每个人同星球相连,边权为1,每个星球通汇点T相连,边权即承受能力。最后再跑一边dinic。
然而人们发现,这样一来铁定会超时(点太多了)。所以,我们采取了一个缩点的策略,将去同一个星球的人缩成了一个点,这样一来,每个人同星球的边权自然就不是1,而是人数。
下面我们看看代码:
#include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<queue> #include<cmath> using namespace std; #define Del(a,b) memset(a,b,sizeof(a)) const int N=1210; const int inf=0x3f3f3f3f; const double esp=1e-9; int n,m; struct Node { int from,to,cap,flow; }; vector<int> v[N]; int sum[N]; vector<Node> e; int vis[N]; //构建层次图 int cur[N]; int MIN(int x,int y) { return x>y?y:x; } void add_Node(int from,int to,int cap) { Node tmp1,tmp2; tmp1.from=from,tmp1.to=to,tmp1.cap=cap,tmp1.flow=0; e.push_back(tmp1); tmp2.from=to,tmp2.to=from,tmp2.cap=0,tmp2.flow=0; e.push_back(tmp2); int tmp=e.size(); v[from].push_back(tmp-2); v[to].push_back(tmp-1); } bool bfs(int s,int t) { Del(vis,-1); queue<int> q; q.push(s); vis[s] = 0; while(!q.empty()) { int x=q.front(); q.pop(); int i; for(i=0;i<v[x].size();i++) { Node tmp = e[v[x][i]]; if(vis[tmp.to]<0 && tmp.cap>tmp.flow) //第二个条件保证 { vis[tmp.to]=vis[x]+1; q.push(tmp.to); } } } if(vis[t]>0) return true; return false; } int dfs(int o,int f,int t) { if(o==t || f==0) //优化 return f; int a = 0,ans=0; int &i = cur[o]; for(;i<v[o].size();i++) //注意前面 ’&‘,很重要的优化 { Node &tmp = e[v[o][i]]; if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,MIN(f,tmp.cap-tmp.flow),t))>0) { tmp.flow+=a; e[v[o][i]^1].flow-=a; //存图方式 ans+=a; f-=a; if(f==0) //注意优化 break; } } return ans; //优化 } int dinci(int s,int t) { int ans=0; while(bfs(s,t)) { Del(cur,0); int tm=dfs(s,inf,t); ans+=tm; } return ans; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int i,j; memset(sum,0,sizeof(sum)); int s=0,x,t=m+(1<<m)+3,nn=(1<<m)+2; for(i=1;i<=n;i++) { int tmp=0; for(j=0;j<m;j++) { scanf("%d",&x); if(x) tmp+=(1<<j); } sum[tmp]++; } for(i=0;i<(1<<m);i++) { if(sum[i]) { add_Node(s,i+1,sum[i]); for(j=0;j<m;j++) { if(i&(1<<j)) add_Node(i+1,nn+j,sum[i]); } } } for(i=0;i<m;i++) { scanf("%d",&x); add_Node(nn+i,t,x); } int ans=dinci(s,t); //printf("%d\n",ans); if(ans==n) printf("YES\n"); else printf("NO\n"); for(i=0;i<=t;i++) v[i].clear(); e.clear(); } return 0; }
以上
阅读全文
1 0
- HDOJ 3605 Escape 解题报告 二分图匹配 dinic 网络流
- HDOJ 3605 - Escape 二分图多重匹配
- hdoj 3605 Escape 【中等最大流 | 二分图多重匹配】
- POJ 2239 Selecting Courses(二分图匹配——匈牙利算法||网络流)解题报告
- 解题报告-HDOJ-2063(最大二分匹配-匈牙利算法)
- HDU 3605 Escape(网络流 + 状压简化 | 二分图多重匹配)
- hdu 3605 Escape (二分图多重匹配)
- hdu 3605 Escape【二分图多重匹配】
- HDU 3605 Escape【二分图多重匹配】
- hdu 3605 Escape (二分图匹配)
- nyoj-239 月老的难题 (二分图匹配—匈牙利算法 && 网络流—Dinic算法)
- 【二分图匹配】 hdu3605 Escape
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
- HDU 3065 Escape(简单网络流 二分图多重匹配)
- hdu 2819 Swap 二分图匹配 匈牙利算法 解题报告
- hdu 2063 过山车 二分图匹配 匈牙利算法 解题报告
- HDU 2063 二分图最大匹配 解题报告
- BZOJ 4950 [Wf 2017] 二分图最大匹配 解题报告
- SDIO驱动(14)card的CIS读取及解析
- 自定义MVC
- BZOJ 3697: 采药人的路径 树的点分治
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现
- Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface com.mysql.jdbc.Co
- HDOJ 3605 Escape 解题报告 二分图匹配 dinic 网络流
- java进制转换
- 扩展.NET 2.0标准配置文件
- 使用 Tango 开发 Unity 应用的原理
- Huffman树
- System.Drawing.Graphics读取带有索引的图片及思考
- 自动登陆网站
- 匈牙利算法+二分答案 BZOJ 4443 小凸玩矩阵 SCOI 2015 题解
- 第2章 windows下搭建开发环境