【bzoj3171】[Tjoi2013]循环格

来源:互联网 发布:世界最快的数学计算法 编辑:程序博客网 时间:2024/05/13 09:44

题目描述:
一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子。每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0)。给定一个起始位置(r,c)
,你可以沿着箭头防线在格子间行走。即如果(r,c)是一个左箭头,那么走到(r,c-1);如果是右箭头那么走到(r,c+1);如果是上箭头那么走到(r-1,c);如果是下箭头那么走到(r+1,c);每一行和每一列都是循环的,即如果走出边界,你会出现在另一侧。
一个完美的循环格是这样定义的:对于任意一个起始位置,你都可以i沿着箭头最终回到起始位置。如果一个循环格不满足完美,你可以随意修改任意一个元素的箭头直到完美。给定一个循环格,你需要计算最少需要修改多少个元素使其完美。
输入:
第一行两个整数R,C。表示行和列,接下来R行,每行C个字符LRUD,表示左右上下。
输出:
一个整数,表示最少需要修改多少个元素使得给定的循环格完美
样例输入:
3 4
RRRD
URLL
LRRR
样例输出:
2
题解:
这题有两种构图方法。首先,两种方法根据出入度都为1来构图。
第一种:
(1)将所有的点拆成两个点,将一个点连向源点S,另一个点连向汇点T,连的边都是容量为1,费用为0的。代表了每个点的出入度都是1 。
(2)一个点向四周的另一类点连出一条容量为1费用为0或1(如果无需修改就是0,否则需要修改就是1)的边。
(3)跑最小费用最大流
第二种:
(1)拆点同上,但连的边的容量为该点原本的入(出)度。
(2)对于每个方格(i, j),我们假设原本连的点为(x,y),那么把这个连向的点的第二类点向其他三个没连的点的第一类点连边,容量为1,费用为1。每个第一类点和第二类点之间都连一条容量为1,费用为0的边。
(3)同第一种
代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#ifdef WIN32    #define LL "%I64d"#else    #define LL "%lld"#endif#ifdef CT    #define debug(...) printf(__VA_ARGS__)    #define setfile() #else    #define debug(...)    #define filename ""    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);#endif#define R register#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1 << 15], *S = B, *T = B;inline int FastIn(){    R char ch; R int cnt = 0; R bool minus = 0;    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;    ch == '-' ? minus = 1 : cnt = ch - '0';    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';    return minus ? -cnt : cnt;}#define maxn 20#define maxcnt 1010#define maxm 100010struct Edge{    int from, to, w, c;    Edge *next, *rev;}*last[maxcnt], *prev[maxcnt], e[maxm], *ecnt = e;int opt[maxn][maxn], id[maxn][maxn], s, t, ans;int dis[maxcnt];bool vis[maxcnt];const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};#define cmod(_a, _b) ((_a) % (_b) == 0 ? (_b) : (_a) % (_b))inline void link(R int _a, R int _b, R int _w, R int _c){    *++ecnt = (Edge) {_a, _b, _w, _c, last[_a], ecnt + 1}; last[_a] = ecnt;    *++ecnt = (Edge) {_b, _a, 0, -_c, last[_b], ecnt - 1}; last[_b] = ecnt;}#define INF 23333333std::queue <int> q;inline bool spfa(){    for (R int i = 0; i <= t; ++i) dis[i] = INF;    dis[s] = 0; vis[s] = 1; q.push(s);    while (!q.empty())    {        R int now = q.front(); q.pop();        for (R Edge *iter = last[now]; iter; iter = iter -> next)        {            if (iter -> w && iter -> c + dis[now] < dis[iter -> to])            {                dis[iter -> to] = iter -> c + dis[now];                prev[iter -> to] = iter;                if (!vis[iter -> to])                {                    vis[iter -> to] = 1;                    q.push(iter -> to);                }            }        }        vis[now] = 0;    }    return dis[t] != INF;}inline void mcmf(){    R int x = INF;    for (R Edge *iter = prev[t]; iter; iter = prev[iter -> from])        cmin(x, iter -> w);    for (R Edge *iter = prev[t]; iter; iter = prev[iter -> from])    {        ans += x * iter -> c;        iter -> w -= x;        iter -> rev -> w += x;    }}int main(){    R int n = FastIn(), m = FastIn(), cnt = 0;    for (R int i = 1; i <= n; ++i)        for (R int j = 1; j <= m; ++j)        {            id[i][j] = ++cnt;            R char ch;            while (ch = getc(), ch < 'A' || ch > 'Z');            if (ch == 'U') opt[i][j] = 0;            if (ch == 'D') opt[i][j] = 1;            if (ch == 'L') opt[i][j] = 2;            if (ch == 'R') opt[i][j] = 3;        }    s = 0; t = cnt << 1 | 1;    for (R int i = 1; i <= n; ++i)        for (R int j = 1; j <= m; ++j)        {            link(s, id[i][j], 1, 0);            link(id[i][j] + cnt, t, 1, 0);            for (R int k = 0; k < 4; ++k)            {                R int nx = i + dx[k], ny = j + dy[k];                link(id[i][j], id[cmod(nx, n)][cmod(ny, m)] + cnt, 1, k == opt[i][j] ? 0 : 1);            }        }    while (spfa()) mcmf();    printf("%d\n",ans );    return 0;}
0 0