博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj 3171: [Tjoi2013]循环格 最小费用最大流
阅读量:4316 次
发布时间:2019-06-06

本文共 1984 字,大约阅读时间需要 6 分钟。

题目大意:

题解:

首先我们很容易发现一个结论:

出现完美循环当且仅当所有点的出入度均为1
所以利用这个性质,我们将每个点拆成两个点
S连向出点,入点连向T,然后出点向上下左右的入点连边
跑最小费用最大流即可.

#include 
#include
#include
#include
using namespace std;typedef long long ll;inline void read(int &x){ x=0;char ch;bool flag = false; while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true; while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;}inline int cat_max(const int &a,const int &b){return a>b ? a:b;}inline int cat_min(const int &a,const int &b){return a
dis[u] + G[i].cost && G[i].cap){ dis[v] = dis[u] + G[i].cost; flow[v] = min(flow[u],G[i].cap); p[v] = i; if(!inq[v]){ q[++r % lim] = v; inq[v] = true; } } }inq[u] = false; }if(dis[T] == dis[0]) return false; ans += dis[T]*flow[T]; for(int u = T;u != S;u = G[p[u]^1].to){ G[p[u]].cap -= flow[T],G[p[u]^1].cap += flow[T]; } return true;}#undef v#define f(x,y) ((x-1)*m + y)int dx[] = {0,0,1,-1};int dy[] = {1,-1,0,0};inline int id(const char &ch){ if(ch == 'U') return 3; if(ch == 'D') return 2; if(ch == 'L') return 1; if(ch == 'R') return 0; return -1;}int main(){ int n,m;read(n);read(m); S = maxnode - 5;T = S+1; char ch; for(int i=1,x;i<=n;++i){ for(int j=1;j<=m;++j){ insert(f(i,j)<<1,T,1,0); insert(S,f(i,j)<<1|1,1,0); while(ch=getchar(),ch<'!'); x = id(ch);if(x == -1) assert(0); for(int k=0;k<4;++k){ int nx = i + dx[k];if(nx == n+1) nx = 1;if(nx == 0) nx = n; int ny = j + dy[k];if(ny == m+1) ny = 1;if(ny == 0) ny = m; insert(f(i,j)<<1|1,f(nx,ny)<<1,1,x != k); } } } while(spfa()); printf("%d\n",ans); getchar();getchar(); return 0;}

转载于:https://www.cnblogs.com/Skyminer/p/6435542.html

你可能感兴趣的文章
SpringMVC FormTag resolve action
查看>>
用U盘安装Linux图解
查看>>
webpack版本升级问题
查看>>
第三天实习报告
查看>>
作业 2.6
查看>>
《JavaScript DOM 编程艺术》 学习笔记
查看>>
文本控件
查看>>
元组的特性
查看>>
C#事件的基本使用
查看>>
ssh中需要增加IntrospectorCleanupListener监听器
查看>>
duilib写个三国杀?
查看>>
GateWay程序分析02_IAP_FLASH.H
查看>>
Stone Crusher Used in High-speed Railway Construction
查看>>
Powerful Function of Hammer Crushers Two
查看>>
an error occurred during the file system check错误的解决
查看>>
logstash json和rubydebug 第次重启logstash都会把所有的日志读完 而不是只读入新输入的内容...
查看>>
实验吧-web-Guess Next Session(session简介)
查看>>
C语言i++和++i的区别和指针*(a++)和*(++a)的区别
查看>>
在一个CommandField中为删除按钮设置OnClientClick属性
查看>>
Linux常用命令-1
查看>>