bzoj 3171 [Tjoi2013]循环格(MCMF)
【题目链接】
http://www.lydsy.com/JudgeOnline/problem.php?id=3171
【题意】
给定一个方向矩阵,要求改变最少的格子,使得任意一个点都在一个环中。
【思路】
任意一个点位于一个环中,即等价于所有的点都有且仅有一个后继。
对于一个点构建X Y结点。
连边(S,Xi,1,0),(Yi,T,1,0)。对于原来可以转移到的点连边(Xi,Yj,1,0),对于需要变换才能转移到的连边(Xi,Yk,1,1)。
求最小费用最大流。最大流保证了每一个点都会有一个后继,既满足在一个环中的条件,在此基础上费用最小。
【代码】
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define trav(u,i) for(int i=front[u];i;i=e[i].nxt)
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long ll;
const int N = 1e3+;
const int inf = 1e9; ll read() {
char c=getchar();
ll f=,x=;
while(!isdigit(c)) {
if(c=='-') f=-; c=getchar();
}
while(isdigit(c))
x=x*+c-'',c=getchar();
return x*f;
} struct Edge {
int u,v,cap,flow,cost;
};
struct MCMF {
int n,m,s,t;
int a[N],p[N],inq[N],d[N];
vector<Edge> es;
vector<int> g[N];
queue<int> q;
void init(int n) {
this->n=n;
es.clear();
FOR(i,,n) g[i].clear();
}
void AddEdge(int u,int v,int w,int c) {
es.push_back((Edge){u,v,w,,c});
es.push_back((Edge){v,u,,,-c});
int m=es.size();
g[u].push_back(m-);
g[v].push_back(m-);
}
int spfa(int s,int t,int& flow,int& cost) {
memset(inq,,sizeof(inq));
FOR(i,,n) d[i]=inf;
inq[s]=; d[s]=; a[s]=inf; p[s]=;
q.push(s);
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
FOR(i,,(int)g[u].size()-) {
Edge& e=es[g[u][i]];
int v=e.v;
if(d[v]>d[u]+e.cost && e.cap>e.flow) {
d[v]=d[u]+e.cost;
a[v]=min(a[u],e.cap-e.flow);
p[v]=g[u][i];
if(!inq[v])
inq[v]=,q.push(v);
}
}
}
if(d[t]==inf) return ;
flow+=a[t]; cost+=a[t]*d[t];
for(int x=t;x!=s;x=es[p[x]].u) {
es[p[x]].flow+=a[t];
es[p[x]^].flow-=a[t];
}
return ;
}
void mcmf(int s,int t,int&flow,int&cost) {
flow=cost=;
while(spfa(s,t,flow,cost)) ;
}
} mc; const char dir[]={'L','R','U','D'};
const int dx[]={,,-,};
const int dy[]={-,,,};
int n,m,S,T;
char mp[N][N]; int id(int x,int y) { return (x-)*m+y; } int main()
{
n=read(),m=read();
FOR(i,,n) scanf("%s",mp[i]+);
mc.init(n*m*+);
S=,T=n*m*+;
FOR(i,,n) {
FOR(j,,m) {
FOR(d,,) {
int x=i+dx[d],y=j+dy[d];
x=(x-+n)%n+,y=(y-+m)%m+;
if(dir[d]==mp[i][j])
mc.AddEdge(id(i,j),id(x,y)+n*m,,);
else mc.AddEdge(id(i,j),id(x,y)+n*m,,);
}
mc.AddEdge(S,id(i,j),,);
mc.AddEdge(id(i,j)+n*m,T,,);
}
}
int cost,flow;
mc.mcmf(S,T,flow,cost);
printf("%d",cost);
return ;
}
bzoj 3171 [Tjoi2013]循环格(MCMF)的更多相关文章
- Bzoj 3171: [Tjoi2013]循环格 费用流
3171: [Tjoi2013]循环格 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 741 Solved: 463[Submit][Status][ ...
- BZOJ 3171 [Tjoi2013]循环格(费用流)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3171 [题目大意] 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子. 每 ...
- bzoj 3171: [Tjoi2013]循环格
#include<cstdio> #include<iostream> #include<cstring> #define M 10000 #define inf ...
- bzoj 3171: [Tjoi2013]循环格 最小费用最大流
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3171 题解: 首先我们很容易发现一个结论: 出现完美循环当且仅当所有点的出入度均为1 所 ...
- 3171. [TJOI2013]循环格【费用流】
Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位置(r,c) ,你可以沿着箭头防线在格 ...
- BZOJ_3171_[Tjoi2013]循环格_最小费用最大流
BZOJ_3171_[Tjoi2013]循环格_最小费用最大流 Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为 ...
- [Tjoi2013]循环格
[Tjoi2013]循环格 2014年3月18日1,7500 Description Input 第一行两个整数R,C.表示行和列,接下来R行,每行C个字符LRUD,表示左右上下. Output 一个 ...
- 洛谷 P3965 [TJOI2013]循环格 解题报告
P3965 [TJOI2013]循环格 题目背景 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子. 每个元素有一个坐标(行,列),其中左上角元素坐标为\((0,0)\).给定一个起始位\ ...
- 【BZOJ 3171】 [Tjoi2013]循环格
Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位置(r,c) ,你可以沿着箭头防线在格 ...
随机推荐
- jQuery编程基础精华03(RadioButton操作,事件,鼠标)
RadioButton操作 取得RadioButton的选中值,被选中的radio只有一个值,所以直接用val() $('#btn1').click(function () { ...
- yii2 学习中
属性: public function __get($name) // 这里$name是属性名 { $getter = 'get' . $name; // getter函数的函数名 if (metho ...
- UVa 10075 - Airlines
航线算球面距离,需要经纬度转空间坐标. 任意两点间距离用Floyd求出来,查询时直接查表. #include <cstdio> #include <map> #include ...
- ehcache版本冲突
以ehchache-core2.5为分水岭 缓存版本问题 版本不一样 配置不一样 ehcache-core-2.4.3.jar 与 ehcache-core-2.6.6 一 Caused by: n ...
- WCF-学习笔记概述之计算服务(1)
关于WCF的介绍,在此不再赘述,其他地方应有尽有.直接开始实例,第一个实例以一个简单的计算服务为例,本人是学习了蒋金楠的<WCF全面解析>. 1.构建解决方案 Interface:用于定义 ...
- Android studio中Rendering Problems不能可视化操作的解决办法
出现:Rendering Problems the following classes could not be found:android.support.v7.internal.widget.Ac ...
- perl基本语法
标量 标量是 Perl 中最简单的数据类型.大多数的标量是数字(如 255 或 3.25e20)或者字符串(如 hello或者盖茨堡地址). 数字 perl中所有数字内部的格式都是双精度浮点数. 浮点 ...
- Pod::Executable pull
使用cocoapods 的时候遇到了以下错误:[!] Pod::Executable pull Updating eaf98af..ba3c030 error: Your local changes ...
- 函数buf_pool_init_instance
buff_pool_t 结构体 详见 /********************************************************************//** Initial ...
- BootStrap弹窗
效果图: 注意引入的文件,js文件要在前面 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在 ...