Codeforces Gym 100203I I - I WIN 网络流最大流
I - I WIN
Time Limit: 2 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87954#problem/I
Description
Given an n × m rectangular tile with each square marked with one of the letters 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 L-shaped ones, and the triominoes can't overlap.
Input
First line contains two integers n and m with 1 ≤ m, n ≤ 22. The next n lines contain m characters each (only the letters W, I and N).
Output
Output a single integer: the maximum number of nonoverlapping WIN-triominoes.
Sample Input
4 4
WIIW
NNNN
IINN
WWWI
Sample Output
5
HINT
题意
给你一个n*m的矩形,最多能找出多少不重复的WIN块?
题解:
建图方法:
S-1-W-1-I-1-I-1-N-T
注意点考虑到有一个I可能会与两个不同位置的W,N相连
还有就是模版6666
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************
namespace NetFlow
{
const int MAXN=,MAXM=,inf=1e9;
struct Edge
{
int v,c,f,nx;
Edge() {}
Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
void init(int _n)
{
N=_n,sz=; memset(G,-,sizeof(G[])*N);
}
void link(int u,int v,int c)
{
E[sz]=Edge(v,c,,G[u]); G[u]=sz++;
E[sz]=Edge(u,,,G[v]); G[v]=sz++;
}
int ISAP(int S,int T)
{//S -> T
int maxflow=,aug=inf,flag=false,u,v;
for (int i=;i<N;++i)cur[i]=G[i],gap[i]=dis[i]=;
for (gap[S]=N,u=pre[S]=S;dis[S]<N;flag=false)
{
for (int &it=cur[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+)
{
if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
pre[v]=u,u=v; flag=true;
if (u==T)
{
for (maxflow+=aug;u!=S;)
{
E[cur[u=pre[u]]].f+=aug;
E[cur[u]^].f-=aug;
}
aug=inf;
}
break;
}
}
if (flag) continue;
int mx=N;
for (int it=G[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[E[it].v]<mx)
{
mx=dis[E[it].v]; cur[u]=it;
}
}
if ((--gap[dis[u]])==) break;
++gap[dis[u]=mx+]; u=pre[u];
}
return maxflow;
}
bool bfs(int S,int T)
{
static int Q[MAXN]; memset(dis,-,sizeof(dis[])*N);
dis[S]=; Q[]=S;
for (int h=,t=,u,v,it;h<t;++h)
{
for (u=Q[h],it=G[u];~it;it=E[it].nx)
{
if (dis[v=E[it].v]==-&&E[it].c>E[it].f)
{
dis[v]=dis[u]+; Q[t++]=v;
}
}
}
return dis[T]!=-;
}
int dfs(int u,int T,int low)
{
if (u==T) return low;
int ret=,tmp,v;
for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
{
if (dis[v=E[it].v]==dis[u]+&&E[it].c>E[it].f)
{
if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
{
ret+=tmp; E[it].f+=tmp; E[it^].f-=tmp;
}
}
}
if (!ret) dis[u]=-; return ret;
}
int dinic(int S,int T)
{
int maxflow=,tmp;
while (bfs(S,T))
{
memcpy(cur,G,sizeof(G[])*N);
while (tmp=dfs(S,T,inf)) maxflow+=tmp;
}
return maxflow;
}
}
using namespace NetFlow;
char mp[][];
int ss[][]={-,,,,,,,-};
int n,m;
int main()
{
init();
n=read();
m=read();
for(int i=; i<=n; i++)
{
scanf("%s",mp[i]+);
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
if(mp[i][j]=='I'){
link((i-)*m+j,(i-)*m+j+,);
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
if(mp[i][j]=='W')
{
//mpp[n*m+1][(i-1)*m+j]=1;
link(n*m++,(i-)*m+j,);
for(int e=; e<; e++)
if(mp[i+ss[e][]][j+ss[e][]]=='I')
{ link((i-)*m+j,(i+ss[e][]-)*m+j+ss[e][],);
}
}
if(mp[i][j]=='I')
{
for(int e=; e<; e++)
if(mp[i+ss[e][]][j+ss[e][]]=='N')
{
link((i-)*m+j+,(i+ss[e][]-)*m+j+ss[e][],);
}
}
if(mp[i][j]=='N')link((i-)*m+j,n*m++,);
}
}
cout<<dinic(n*m++,n*m+++)<<endl;
return ;
}
Codeforces Gym 100203I I - I WIN 网络流最大流的更多相关文章
- Codeforces Gym 100203I I WIN 最大流
		原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 首先寻找每个I,然后枚举形状,如果匹 ... 
- Gym - 100203I  I WIN  网络流
		Gym - 100203I I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ... 
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
		一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ... 
- Codeforces Gym 101190M Mole Tunnels - 费用流
		题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ... 
- Codeforces Gym 101623A - 动态规划
		题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ... 
- 【Codeforces Gym 100725K】Key Insertion
		Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ... 
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
		2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ... 
- codeforces gym 100553I
		codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ... 
- CodeForces Gym 100213F Counterfeit Money
		CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ... 
随机推荐
- 通过开源程序同时解决DNS劫持和DNS污染的问题
			我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ... 
- 关于windows系统下 webpack的使用
			最近包子在研究webpack打包,发现,真是个好东西,以前完全不懂,其实很简单,步骤如下: 1.安装webpack嘎嘎嘎嘎~~~ 2.初始化一下 3.这玩意是啥,我不知道,就依葫芦画瓢 4.这玩意是啥 ... 
- VS2010 使用时选择代码或双击时出错,点击窗口按钮后VS自动重启问题
			VS2010 使用时选择代码或双击时出错崩溃,点击窗口按钮后VS自动重启问题 下载补丁,打上补丁之后,重启电脑,解决了问题. WindowsXP的下载地址:Windows XP 更新程序 (KB971 ... 
- [Effective JavaScript 笔记]第35条:使用闭包存储私有数据
			js的对象系统并没有特别鼓励或强制信息隐藏.所有的属性名都是一个字符串,任意一个程序都可以简单地通过访问属性名来获取相应的对象属性.例如,for...in循环.ES5的Object.keys()和Ob ... 
- PHP 基础语法实例及注意事项
			<?$varint = 1;$varinteger = "test";$varstring = "tes";$varbool = true;$varflo ... 
- Sort List
			采用归并排序,通过定义快.慢两个指针来找到中点,再采用之前的排序算法进行归并. ListNode *listSort(ListNode *head) { //定义快慢指针,找到链表中心 ListNod ... 
- asp.net 网站 或者web Api 发布
			asp.net 发布iis时可能遇到的内部服务错误常见的有两种: 1.如下图,500.19 Internal Server Error(内部服务错误) 这种错误可能是由于本机的注册表中的asp.net ... 
- NYOJ 106背包问题
			http://acm.nyist.net/JudgeOnline/problem.php?pid=106 背包问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现 ... 
- 九度 OJ1008  hdu 3790
			#include<stdio.h> #include<string.h> struct node { int d; int p; }g[][]; #define inf 0x3 ... 
- 用php实现百度网盘图片直链的代码分享
			第一种代码:代码量较少通过正则表达式获取百度网盘的文件真实地址,来实现直链的效果 将下面的代码保存为downbd.php 复制代码代码如下: <?php $canshu=$_SERVER[&qu ... 
