Description

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

WIIW
NNNN
IINN
WWWI NINWN
INIWI
WWWIW
NNNNN
IWINN

Sample Output

5
5 以前一直不会网络流,直到现在遇到了网络流的题目才决定学一学。
这题就相当与我的网络流入门题吧。
这题其实是一个非常容易的网络流题目,只是我以前都不会。
所以觉得难,多看一些网络流的题目,多了解一些套路就可以了。
这里我用的是我的dinic模板。
现在自己仔细讲讲这题如何做,
题意:给你一张图,求出有几个WIN 。
网络流的难点就在构图上面,比较各种网络流模板差不多,都是当做
黑箱使用,如何构图就是一个艺术性的事情了。
其实这题类似于飞行员匹配问题,只是由两点匹配变成了三点匹配。
其实就是想办法转化为两点匹配,就是类似于二分图。
W是头,N是尾,所以主要处理的就是I,
主要说明一下构图原理,建立一个源点连接到所有的W,然后一个终点连接所有的N
这里最巧妙的就是in和out,
源点和out 【n*m,2*n*m-1】相连
终点和in 【0,n*m-1】 相连
所以这里处理I 就是将 I 作为连接 in 和 out 的桥梁

if (tu[i][j] == 'I') {
            for (int k = 0 ; k < 4 ; k++) {
                  int nx = i + dx[k];
                  int ny = j + dy[k];
                  if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                  if (tu[nx][ny] == 'W') f.add(nx * m + ny + out, i * n + j + in, 1) ;
                  if (tu[nx][ny] == 'N') f.add(i * m + j + out, nx * m + ny + in, 1) ;
            }
   }

这个就是核心代码了。

想必讲到这里 ,已经是非常非常详细了。

上代码

#include <vector>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
const int maxn = 1e4 + ;
const int INF = 1e9 + ;
struct node {
int from, to, cap, flow;
};
struct Dinic {
int n, m, s, t;
vector<node>nodes;
vector<int>g[maxn];
int vis[maxn];
int d[maxn];
int cur[maxn];
void clearall(int n) {
for (int i = ; i < n ; i++) g[i].clear();
nodes.clear();
}
void clearflow() {
int len = nodes.size();
for (int i = ; i < len ; i++) nodes[i].flow = ;
}
void add(int from, int to, int cap) {
nodes.push_back((node) {
from, to, cap,
});
nodes.push_back((node) {
to, from, ,
});
m = nodes.size();
g[from].push_back(m - );
g[to].push_back(m - );
}
bool bfs() {
memset(vis, , sizeof(vis));
queue<int>q;
q.push(s);
d[s] = ;
vis[s] = ;
while(!q.empty()) {
int x = q.front();
q.pop();
int len = g[x].size();
for (int i = ; i < len ; i++) {
node &e = nodes[g[x][i]];
if (!vis[e.to] && e.cap > e.flow ) {
vis[e.to] = ;
d[e.to] = d[x] + ;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == ) return a;
int flow = , f, len = g[x].size();
for (int &i = cur[x] ; i < len ; i++) {
node & e = nodes[g[x][i]];
if (d[x] + == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > ) {
e.flow += f;
nodes[g[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int maxflow(int a, int b) {
s = a;
t = b;
int flow = ;
while(bfs()) {
memset(cur, , sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
vector<int>mincut() {
vector<int>ans;
int len = nodes.size();
for (int i = ; i < len ; i++) {
node & e = nodes[i];
if ( vis[e.from] && !vis[e.to] && e.cap > ) ans.push_back(i);
}
return ans;
}
void reduce() {
int len = nodes.size();
for (int i = ; i < len ; i++) nodes[i].cap -= nodes[i].flow;
}
} f;
int ans(vector<string> &tu ) {
int n = tu.size(), m = tu[].length();
int source = * n * m, sink = * n * m + ;
int in = , out = n * m;
int dx[] = {, , , -};
int dy[] = {, , -, };
f.clearall( * n * m + );
f.clearflow();
for (int i = ; i < n ; i++) {
for (int j = ; j < m ; j++) {
f.add(i * m + j + in, i * m + j + out, );
if (tu[i][j] == 'W') f.add(source, i * m + j + in, );
if (tu[i][j] == 'I') {
for (int k = ; k < ; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx < || nx >= n || ny < || ny >= m) continue;
if (tu[nx][ny] == 'W') f.add(nx * m + ny + out, i * m + j + in, ) ;
if (tu[nx][ny] == 'N') f.add(i * m + j + out, nx * m + ny + in, ) ;
}
}
if (tu[i][j] == 'N') f.add(i * m + j + out, sink, );
}
}
return f.maxflow(source, sink);
}
int main() {
while() {
string s;
vector<string> tu;
while(getline(cin, s)) {
if (s.length() == ) break;
tu.push_back(s);
}
if (tu.size() == ) break;
printf("%d\n", ans(tu));
}
return ;
}

Tile Cut~网络流入门题的更多相关文章

  1. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...

  2. POJ-1273Drainage Ditches(网络流入门题,最大流)

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...

  3. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  4. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

  5. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. 网络流入门-POJ1459PowerNetwork-Dinic模板

    (我有什么错误或者你有什么意见,欢迎留言或私聊!谢谢!) (Ps:以前听说过网络流,想着以后再学,这次中南多校赛也碰到有关网络流的题目,想着这两天试着学学这个吧~~ 这是本人网络流入门第二题,不知道怎 ...

  7. Cogs 732. [网络流24题] 试题库(二分图)

    [网络流24题] 试题库 ★★ 输入文件:testlib.in 输出文件:testlib.out 评测插件 时间限制:1 s 内存限制:128 MB «问题描述: 假设一个试题库中有n道试题.每道试题 ...

  8. Cogs 739. [网络流24题] 运输问题(费用流)

    [网络流24题] 运输问题 ★★ 输入文件:tran.in 输出文件:tran.out 简单对比 时间限制:1 s 内存限制:128 MB «问题描述: «编程任务: 对于给定的m 个仓库和n 个零售 ...

  9. Cogs 727. [网络流24题] 太空飞行计划(最大权闭合子图)

    [网络流24题] 太空飞行计划 ★★☆ 输入文件:shuttle.in 输出文件:shuttle.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] W 教授正在为国家航天中心计 ...

随机推荐

  1. SSH远程登录和端口转发详解

     SSH远程登录和端口转发详解   介绍 SSH 是创建在应用层和传输层基础上的安全协议,为计算机上的 Shell(壳层)提供安全的传输和使用环境. SSH 只是协议,有多种实现方式,本文基于其开源实 ...

  2. Spring 中的文件上传与下载控制

    先创建根应用上下文配置,WebDemo/src/main/java/com/seliote/webdemo/config/RootContextConfig.java package com.seli ...

  3. Markdown 基本用法

    声明:引自 http://www.cnblogs.com/hnrainll/p/3514637.html ,感谢!   1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置 ...

  4. 4269: 再见Xor

    4269: 再见Xor 链接 分析: 和SGU 275唯一不同的就是需要求出次小值,那么异或出最大值的所有元素中,找到最小的,去除即可. 代码: #include<bits/stdc++.h&g ...

  5. C#属性默认值设置

    关于在MVC中view中设置默认值,可以象如下设置: 1.关于VIEWMODEL的部分 如果是C#  6.0,网上资料查到说可以 如果语法不支持,只能改回.net 2.0的写法. public cla ...

  6. 【数据库】 SQL 常用语句

    [数据库] SQL 常用语句 1.批量导入 INSERT INTO Table2(field1,field2,...) SELECT value1,value2,... FROMTable1 要求目标 ...

  7. Pc移植到Mac的技术细节

    1.样式不对: 2.布局不对: 3.Mac的菜单替换PC的菜单: Mac的菜单替换PC的菜单: 1)左上角图标没有手动添加且不需要添加的情况下出现,而且点击是Help菜单内容: 2)把HelpBtn和 ...

  8. es6严格模式需要注意的地方

    1.块级函数 "use strict"; if (true) { function f() { } // 语法错误 } es5中严格模式下禁止声明块级函数,而在es6的严格模式中可 ...

  9. truffle的调用nodeJs的问题

    Truffle3.0集成NodeJS并完全跑通(附详细实例,可能的错误) 升级到Truffle3.0 如果之前安装的是Truffle2.0版本,需要主动升级到Truffle3.0,两者的语法变化有点大 ...

  10. Mapper的方式总结

    Mapper的方式总结: <mappers> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <package name=&quo ...