Tile Cut(2018湖南第三次多校,K题,网络流拆点)

原创 2018年04月17日 15:34:49

Problem K — limit 10 seconds
Tile Cut
When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like to
play a little game with parchment and pen to decide who buys the next round. The game works
as follows:
Given an
m
×
n
rectangular tile with each square marked with one of the incantations
W
,
I
, and
N
, find the maximal number of triominoes that can be cut from this tile such that the triomino has
W
and
N
on the ends and
I
in the middle (that is, it spells
WIN
in some order). Of course the only
possible triominoes are the one with three squares in a straight line and the two ell-shaped ones.
The Hobbit that is able to find the maximum number wins and chooses who buys the next round.
Your job is to find the maximal number.
Side note: Sam and Pippin tend to buy the most rounds of ale when they play this game, so
they are lobbying to change the game to Rock, Parchment, Sword (RPS)!
Input
Each input file will contain multiple test cases. Each test case consists of an
m
×
n
rectangular
grid (where 1

m, n

30) containing only the letters
W
,
I
, and
N
. Test cases will be separated by
a blank line. Input will be terminated by end-of-file.
Output
For each input test case, print a line containing a single integer indicating the maximum total
number of tiles that can be formed.
Sample Input
Sample Output
WIIW
NNNN
IINN
WWWI
NINWN
INIWI
WWWIW
NNNNN
IWINN
5
5

思路:可能是太久没碰网络流了,没反应出来是网络流,因为每个点最多只能用一次,很容易就知道是要用最大流啦,然后拆点那些思路自然而然就想出来辣(主要是记下板子)
accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MX = 1800+4;
const int MS = 1800*1800+32;
string s[31];
int n,m;
template<class T>
struct Max_Flow
{
    int n;
    int Q[MX],sign;
    int head[MX],level[MX],cur[MX],pre[MX];
    int nxt[MS],pnt[MS],E;
    T cap[MS];
    void init(int n)
    {
        E = 0;
        this->n = n+1;
        fill(head,head+this->n,-1);
    }
    void Add(int from, int to,T c,T rw = 0){
        pnt[E] = to;
        cap[E] = c;
        nxt[E] = head[from];
        head[from] = E++;
        pnt[E] = from;
        cap[E] = rw;
        nxt[E] =head[to];
        head[to] = E++;
    }
    bool BFS(int s,int t)
    {
        sign = t;
        std::fill(level,level+n,-1);
        int *front = Q;
        int *tail = Q;
        *tail++ = t;
        level[t] = 0;
        while(front < tail &&level[s] == -1){
            int u = *front++;
            for(int e = head[u];e!=-1;e = nxt[e]){
                if(cap[e^1] > 0&&level[pnt[e]]<0){
                    level[pnt[e]] = level[u]+1;
                    *tail++ = pnt[e];
                }
            }
        }
        return level[s] != -1;
    }
    void Push(int t,T &flow){
        T mi =INF;
        int p = pre[t];
        for(int p = pre[t];p!=-1;p = pre[pnt[p^1]]){
            mi = std::min(mi,cap[p]);
        }
        for(int p = pre[t];p!=-1;p = pre[pnt[p^1]]){
            cap[p] -= mi;
            if(!cap[p]){
                sign = pnt[p^1];
            }
            cap[p^1] += mi;
        }
        flow += mi;
    }
    void DFS(int u,int t, T &flow){
        if(u==t){
            Push(t,flow);
            return ;
        }
        for(int &e = cur[u];e != -1;e = nxt[e]){
             if(cap[e]>0&&level[u]-1==level[pnt[e]]){
                pre[pnt[e]] = e;
                DFS(pnt[e],t,flow);
                if(level[sign] >level[u]){
                    return;
                }
                sign = t;
             }
        }
    }
    T Dinic(int s,int t){
        pre[s] = -1;
        T flow = 0;
        while(BFS(s,t)){
            std::copy(head,head+n,cur);
            DFS(s,t,flow);
        }
        return flow;
    }
};
struct node2
{
    int x,y;
    int id;
};
Max_Flow<int>F;
vector<node2>W;
vector<node2>N;
vector<node2>I;
void dfs(int x,int y)
{
    int p = 0;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<s[i].size();j++){
            node2 tmp;
            tmp.x = i;
            tmp.y = j;
            tmp.id = p++;
            if(s[i][j]=='W'){
                W.push_back(tmp);
            }
            else if(s[i][j]=='N'){
                N.push_back(tmp);
            }
            else{
                I.push_back(tmp);
            }
        }
    }
}
void slove()
{
    F.init(2*n*m+10);
    W.clear();
    I.clear();
    N.clear();
    dfs(0,0);
    int cnt = 0;
    for(int i = 0;i<W.size();i++){
        F.Add(2*n*m,W[i].id,1);
        for(int j = 0;j<I.size();j++){
            if(W[i].x==I[j].x&&abs(W[i].y-I[j].y)==1){
                F.Add(W[i].id,I[j].id,1);
                cnt++;
            }
            else if(W[i].y==I[j].y&&abs(W[i].x-I[j].x)==1){
                F.Add(W[i].id,I[j].id,1);
                cnt++;
            }
        }
    }
    for(int i = 0;i<I.size();i++){
        F.Add(I[i].id,I[i].id+n*m,1);
    }
    //cout<<W.size()<<' '<<N.size()<<' '<<I.size()<<endl;
    for(int i = 0;i<N.size();i++){
        F.Add(N[i].id,2*n*m+1,1);
        for(int j = 0;j<I.size();j++){
             if(N[i].x==I[j].x&&abs(N[i].y-I[j].y)==1){
                F.Add(I[j].id+n*m,N[i].id,1);
                cnt++;
            }
            else if(N[i].y==I[j].y&&abs(N[i].x-I[j].x)==1){
                F.Add(I[j].id+n*m,N[i].id,1);
                cnt++;
            }
        }
    }
   // cout<<cnt<<endl;
   // cout<<":fwf"<<endl;
    printf("%d\n",F.Dinic(2*n*m,2*n*m+1));

}
int main()
{
    string ss;
    while(true){
    n = 0;
    m = 0;
    while(getline(cin,ss))
    {
        if(ss.size()==0){
            //dfs(0,0);
        //    cout<<n<<' '<<m<<endl;
           break;
        }
        else{
            s[n++] = ss;
            m = ss.size();
        }
    }
    if(n==0) return 0;
    else{
        slove();
    }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w571523631/article/details/79975690

2018 湖南多校(2)----CSU 2037 Mars (后缀自动机 + DFS)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2037 由于博主水平有限,无法解出这道题!查阅了 国科大大佬alpc_qleonar...
  • qq_38786088
  • qq_38786088
  • 2018-04-11 12:38:09
  • 26

2017湖南多校第三场

C(chy) 给出两个n*n的矩阵A,B.判断A*A == B? 构造一个n*1的随机矩阵C。 问题转化为A*A*C == B*C. 即A*(A*C) == B*C。这样复杂度就降低到了n^2...
  • Imnotzox
  • Imnotzox
  • 2017-03-19 16:29:39
  • 228

【网络流24题】火星探险(拆点+费用流)

传送门    火星探险     题意:给定网格,其中放有只能被拿取一次的岩石,求从左上角格点到右下角格点的k条路径,使得拿取的岩石总数最多。I think    最大费用流,将点拆至x,y两个集合....
  • Etta19
  • Etta19
  • 2017-08-28 19:00:49
  • 222

2017湖南多校第二场-20170312

F(1833): Lab Submit Status Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 35 ...
  • algzjh
  • algzjh
  • 2017-03-15 13:40:28
  • 238

2017湖南多校第一场-B(1002): Bones’s Battery

Problem B | limit 5 secondsBones’s BatteryBones is investigating what electric shuttle is appropriat...
  • algzjh
  • algzjh
  • 2017-03-22 18:55:12
  • 402

UVA - 1658(网络流经典拆点方法)

把每个点(除了1,n)之外拆成i和i1,两点间连一条容量为1,费用为零的边,这样可以限定,每个点只被跑到一次,那么之后跑一个最流量为2的最小费用流就可以了。 至于其他边,流量设为1,保证每个边只被跑...
  • playwfun
  • playwfun
  • 2015-09-10 21:36:10
  • 699

F-德玛西亚万岁(状压DP)(2018年全国多校算法寒假训练营练习比赛(第二场))

题目链接:     https://www.nowcoder.com/acm/contest/74/F 题目大意:   给你n*m大小的棋盘,其中1可以放置英雄,0不能放置。而放置英雄的格子的上下...
  • wxw15617488718
  • wxw15617488718
  • 2018-01-30 09:48:57
  • 110

网络流入门2——最大流(关于为什么要拆点的问题)

POJ 3281 Dining (USACO 2007 Open Gold) 题目意思比较简单,就是说现在有 N 只奶牛,F 种食物和 D 种饮料,每只奶牛喜欢其中的一些食物和饮料。现在每种食物和饮...
  • xianpingping
  • xianpingping
  • 2018-01-31 10:17:53
  • 72

[线性规划与网络流24题] 餐巾计划问题

CodeVS 1237。 粗浅地学习了带上下界的网络流的人的代码的时间开销:1472ms。 领悟二分图精髓的人的代码的时间开销:18ms。 两种建图的顶点数相同,我的边数是后者的2/3。但是增广...
  • ruoruo_cheng
  • ruoruo_cheng
  • 2016-05-28 23:32:55
  • 1180

2017第六次多校联合hdu6103

题目题意还是比较好理解的,就是求小于所给m的最大dis的子串最大长度。题解:首先正方向枚举每一个点,固定这个点作为开始的左边界。然后分别从这个左边界和最后一个点开始拿,直到dis>m,这时候更新长度最...
  • meituanwaimai
  • meituanwaimai
  • 2017-08-11 14:37:37
  • 127
收藏助手
不良信息举报
您举报文章:Tile Cut(2018湖南第三次多校,K题,网络流拆点)
举报原因:
原因补充:

(最多只允许输入30个字)