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

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();
    }
    }
}

猜你喜欢

转载自blog.csdn.net/w571523631/article/details/79975690