P4001-[ICPC-Beijing 2006]狼抓兔子【对偶图】
生活随笔
收集整理的这篇文章主要介绍了
P4001-[ICPC-Beijing 2006]狼抓兔子【对偶图】
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
正题
题目链接:https://www.luogu.com.cn/problem/P4001
题目大意
给出一个类似于
的网格图,求起点到终点的最小割。
解题思路
最小割直接跑网络流,然后发现dinicdinicdinic都过不了。(好像加点玄学优化就能过)
然后上点科技,平面图最小割转其补图(对偶图)的最短路
- 平面图:满足所有边不相交的情况下可以被画在平面上的一张图G(V,E)G(V,E)G(V,E)
- 对偶图:将一张平面图的各个区域变成一个点,然后平面图上分割两个区域a,ba,ba,b的边在对偶图上就是连接a,ba,ba,b的一条边。
这题显然是平面图,转换成对偶图就是
画的比较丑,将就着看把。
然后感性理解一下发现结论确实成立。
跑dijdijdij就好了
时间复杂度O(nmlog(nm))O(nm\log\ (nm))O(nmlog (nm))
code
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define mp(x,y) make_pair(x,y) using namespace std; const int N=1010*1010*2; struct node{int to,next,w; }a[N<<4]; int n,m,s,t,tot,ls[N],f[N]; bool v[N]; priority_queue<pair<int ,int > >q; int p(int x,int y,int z) {return ((x-1)*m+y)*2-z;} void addl(int x,int y,int w){a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=w;return; } int dij(){memset(f,0x3f,sizeof(f));f[s]=0;q.push(mp(0,s));while(!q.empty()){int x=q.top().second;q.pop();if(v[x])continue;v[x]=1;for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(f[x]+a[i].w<f[y]){f[y]=f[x]+a[i].w;q.push(mp(-f[y],y));}}}return f[t]; } int main() {scanf("%d%d",&n,&m);s=p(n-1,m-1,0)+1;t=s+1;for(int i=1;i<=n;i++)for(int j=1;j<m;j++){int x;scanf("%d",&x);if(i==1)addl(s,p(i,j,0),x);else if(i==n)addl(p(i-1,j,1),t,x);else addl(p(i-1,j,1),p(i,j,0),x);}for(int i=1;i<n;i++)for(int j=1;j<=m;j++){int x;scanf("%d",&x);if(j==1)addl(p(i,j,1),t,x);else if(j==m)addl(s,p(i,j-1,0),x);else addl(p(i,j,1),p(i,j-1,0),x);}for(int i=1;i<n;i++)for(int j=1;j<m;j++){int x;scanf("%d",&x);addl(p(i,j,0),p(i,j,1),x);}printf("%d\n",dij());return 0; }总结
以上是生活随笔为你收集整理的P4001-[ICPC-Beijing 2006]狼抓兔子【对偶图】的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 游戏电脑顶级配置推荐(游戏电脑顶级配置)
- 下一篇: 51nod1355-斐波那契的最小公倍数