codeforce Gym 100203I I WIN (网络流)
把'I'拆成容量为1一条边,一个入点一个出点,入点和相邻的'W'连一条容量为1的边,出点和相邻的'N'连一条容量为1,所有的'W'和源点连一条容量为1边,所有的'N'和汇点连一条容量为1的边,表示只能用一次。一发网络流就过了。
写了4000B+的贪心,然并卵
#include<bits/stdc++.h>
using namespace std; const int INF = 0x3fffffff;
const int maxn = ;
#define PB push_back
struct Edge
{
int from,to,cap,flow;
}; vector<Edge> edges;
vector<int> G[maxn];
int S ,T ; void AddEdge(int from,int to,int cap)
{
edges.PB(Edge{from,to,cap,});
edges.PB(Edge{to,from,,});
int m = edges.size();
G[from].PB(m-);
G[to].PB(m-);
} bool vis[maxn];
int d[maxn],cur[maxn]; bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(S);
d[S] = ;
vis[S] = true;
while(q.size()){
int u = q.front(); q.pop();
for(int i = ; i < G[u].size(); i++){
Edge &e = edges[G[u][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = true;
d[e.to] = d[u] + ;
q.push(e.to);
}
}
}
return vis[T];
} int dfs(int u,int a)
{
if(u == T || a == ) return a;
int flow = , f;
for(int &i = cur[u]; i < G[u].size(); i++){
Edge &e = edges[G[u][i]];
if(d[u] + == d[e.to] && (f = dfs(e.to,min(a,e.cap-e.flow))) >){
e.flow += f;
edges[G[u][i]^].flow -= f;
flow += f;
a -= f;
if(a == ) break;
}
}
return flow;
} int MaxFlow()
{
int flow = ;
while(bfs()){
memset(cur,,sizeof(cur));
flow += dfs(S,INF);
}
return flow;
} typedef pair<int,int> pii;
#define fi first
#define se second
#define MP make_pair
vector<pii> vec; const int N = ;
char s[N][N];
int id[N][N]; int dx[] = {,,-,};
int dy[] = {,-,,}; int main()
{
//freopen("in.txt","r",stdin);
int n,m; scanf("%d%d",&n,&m);
int Icnt = , Wcnt = , Ncnt = ;
for(int i = ; i <= n; i++){
scanf("%s",s[i]+);
for(int j = ; j <= m; j++){
if(s[i][j] == 'I'){
vec.PB(MP(i,j));
id[i][j] = Icnt++;
}
if(s[i][j] == 'W'){
id[i][j] = Wcnt++;
}
if(s[i][j] == 'N'){
id[i][j] = Ncnt++;
}
}
}
int MW = Icnt* + ;
int MN = MW + Wcnt + ;
S = MN +Ncnt + ; T = MN +Ncnt + ;
for(int i = ; i < vec.size(); i++){
int x = vec[i].fi, y = vec[i].se;
int in = *i+, out = *i+;
AddEdge(in,out,);
for(int k = ; k < ; k++){
int nx = x+dx[k], ny = y+dy[k];
if(s[nx][ny] == 'W'){
int u = id[nx][ny]+MW;
AddEdge(u,in,);
}
if(s[nx][ny] == 'N'){
int v = id[nx][ny]+MN;
AddEdge(out,v,);
}
}
}
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++){
if(s[i][j] == 'W'){
int v = id[i][j]+MW;
AddEdge(S,v,);
}
if(s[i][j] == 'N'){
int u = id[i][j]+MN;
AddEdge(u,T,);
}
}
int ans = MaxFlow();
printf("%d",ans);
return ;
}
codeforce Gym 100203I I WIN (网络流)的更多相关文章
- Gym - 100203I I WIN 网络流
Gym - 100203I I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ...
- Codeforces Gym 100203I I WIN 最大流
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 首先寻找每个I,然后枚举形状,如果匹 ...
- Codeforces Gym 100203I I - I WIN 网络流最大流
I - I WINTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...
- Codeforce Gym 100015I Identity Checker 暴力
Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...
- codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- codeforce gym/100495/problem/F Snake++——DFS应用
emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个: ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ...
- codeforce Gym 100425E The Street Escalator(期望,线性递推)
算数学期望,每个人都可以分开来考虑.Xi表示第i个人跑到另外一边的次数. Xi服从二项分布.概率的和是个二项式,(p+1-p)^T,把二项式展开,p的偶次项是留在原来那一边的概率. 可以用((a+b) ...
- Gym - 101128F Landscaping(网络流)
题意 给你一个\(N×M\)的草地,有高地有低地. 收割机从低地走到高地或者从高地走到低地都要花费用\(A\),你可以花费用\(B\)把一块高地变成低地,或者把一块低地变成高地.收割机每行每列都是必须 ...
随机推荐
- HDOJ-2037
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 【Data Structure & Algorithm】在排序数组中查找和为定值的两个数
在排序数组中查找和为定值的两个数 题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字,要求时间复杂度是O(n).如果有多对数字的和等于输入的数字,输出 ...
- mysql的权限问题SQLException: access denied for @'localhost' (using password: no)
遇到了 SQLException: access denied for @'localhost' (using password: no) 解决办法 grant all privileges o ...
- java8--List转为Map、分组、过滤、求和等操作
利用java8新特性,可以用简洁高效的代码来实现一些数据处理~ 定义1个Apple对象: public class Apple { private Integer id; private String ...
- Warning: The Copy Bundle Resources build phase contains
在编译程序时,遇到了这样的Waring: Warning: The Copy Bundle Resources build phase contains this target's Info.pl ...
- POJ3264 【RMQ基础题—ST-线段树】
ST算法Code: //#include<bits/stdc++.h> #include<cstdio> #include<math.h> #include< ...
- HDU5971【瞎搞】
题意:略(忙着准备文化课...明天期中考啊.... 思路: 正解就是染色,2-sat搞: AC代码(虽然是错误的...数据水(过踏马的也行啊,起码打脸他啊!) 4 3 1 0 1 2 2 3 3 4 ...
- unity3d读写txt
http://www.cnblogs.com/sunet/p/3851353.html?utm_source=tuicool 记录一下昨天用到的技术点:基于android平台unity3d读写txt. ...
- Python Matplotlib 中对于 bar 显示时间的问题
参考: 官方教程: http://matplotlib.org/1.3.1/users/recipes.html http://stackoverflow.com/questions/13515471 ...
- 17..userinfo.txt 文件中存放以下结构:
alex:alex3714 wusir:123456 meet:meet123 1.让用户选择: 1.注册 2.登录 2.用户选择注册就将账号和密码添加到userinfo.txt中,如果用户名存在就提 ...