Uva-1515-Pool construction
来源:互联网 发布:一号店和天猫超市 知乎 编辑:程序博客网 时间:2024/05/16 17:10
这个题刚开始确实没想到是用最小割来做,建图也非常的巧妙,
1、设定源点S,和汇点T.
2、把周围的洞全部变为草,然后对于周围的每个草建立S到t容量为无穷大的边
3、对于其他的草,建立S到该点容量为d的边,对于洞建立该点到汇点容量为f的边
4、对于图中的每个点与它周围建立容量为b的双向边。
根据最小割最大流定理,跑一遍网络流问题应该就可以解决了。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<queue>using namespace std;const int inf=1<<29;const int maxn=100;const int maxm=1e5+100;char map[maxn][maxn];int w,h,d,f,b,ans,e,st,des,head[3000],nxt[maxm],flow[maxm],pnt[maxm];int level[3000];bool vis[3000];queue<int> q;void AddEdge(int u,int v,int f){ pnt[e]=v;nxt[e]=head[u];flow[e]=f;head[u]=e++; pnt[e]=u;nxt[e]=head[v];flow[e]=0;head[v]=e++;}void Init(){ for(int i=0;i<w;i++) { if(map[0][i]=='.') { ans+=f; map[0][i]='#'; } if(map[h-1][i]=='.') { ans+=f; map[h-1][i]='#'; } AddEdge(st,i,inf); AddEdge(st,(h-1)*w+i,inf); } for(int i=0;i<h;i++) { if(map[i][0]=='.') { ans+=f; map[i][0]='#'; } if(map[i][w-1]=='.') { ans+=f; map[i][w-1]='#'; } AddEdge(st,i*w,inf); AddEdge(st,i*w+w-1,inf); } for(int i=1;i<h-1;i++) for(int j=1;j<w-1;j++) { if(map[i][j]=='#') AddEdge(st,i*w+j,d); else AddEdge(i*w+j,des,f); } for(int i=0;i<h;i++) for(int j=0;j<w;j++) { if(j>0) { AddEdge(i*w+j,i*w+j-1,b); AddEdge(i*w+j-1,i*w+j,b); } if(i>0) { AddEdge(i*w+j,(i-1)*w+j,b); AddEdge((i-1)*w+j,i*w+j,b); } }}bool BFS(int st,int des){ memset(level,0,sizeof(level)); level[st]=1; q.push(st); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=nxt[i]) { int v=pnt[i]; if(flow[i]&&!level[v]) { level[v]=level[u]+1; q.push(v); } } } return level[des];}int DFS(int u,int maxf){ if(u==des||!maxf) return maxf; int ans=0; for(int i=head[u],t;i!=-1;i=nxt[i]) if(level[pnt[i]]==level[u]+1&&(t=DFS(pnt[i],min(flow[i],maxf)))) { flow[i]-=t; flow[i^1]+=t; return t; } return level[u]=0;}int maxflow(){ int ans=0; while(BFS(st,des)) while(1) { int val=DFS(st,inf); if(!val) break; ans+=val; } return ans;}int main(){ int T; scanf("%d",&T); while(T--) { ans=0;e=0; memset(head,-1,sizeof(head)); scanf("%d%d",&w,&h); scanf("%d%d%d",&d,&f,&b); for(int i=0;i<h;i++) scanf("%s",map[i]); st=w*h+1,des=w*h+2; Init(); printf("%d\n",maxflow()+ans); } return 0;}
1 0
- Uva-1515-Pool construction
- UVa 1515 Pool construction
- UVA 1515 Pool construction
- UVA 1515Pool construction
- UVA 1515 Pool construction
- 最小割 UVA 1515 Pool construction
- uva 1515 Pool construction(最大流)
- UVA 1515 Pool construction(网络流)
- UVA 1515 Pool construction [最小割]
- UVA 1515 Pool construction(最小割)
- uva 1515 Pool construction(最小割)
- 1515 - Pool construction
- 1515 - Pool construction(最大流最小割模型)
- 【网络流】- LA5905-Pool construction
- UVa 10720 - Graph Construction
- UVa 10720 - Graph Construction
- uva 10720 - Graph Construction
- uva 10720 - Graph Construction
- 工作感悟——从用户中来,到用户中去
- 从xen向kvm切换虚拟机
- cocos2d-x-lua基础系列教程四(lua多继承)
- windows server 2003 远程桌面最大连接数调整
- PHP内核探索:zend_parse_parameters函数
- Uva-1515-Pool construction
- Linux安装MySQL
- cocos2d-x-lua基础系列教程五(lua单例)
- 变量、常量和函数
- STM32基本定时器
- unexpected end of file,设置fileformat即可
- iOS开发UITableView 之代码自定义cell的步骤
- XFire WebService开发快速起步
- 有效防忽悠!5个笔记本选购陷阱