Gym - 100203I I WIN 网络流
题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN。
思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下没过就没改了,,知道了是网络流就好办了。设一个起点一个终点,起点和每个W之间连一条边,N和终点间连一条边,W和I之间连一条边,I和N之间连一条边,不过这里为了避免重复使用同一个I,应改成W到I连一条边,I连一条边到I',再从I'连一条边到N就可以保证最优解了。求一遍最大流即可。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 1005
using namespace std; struct Edge{
int from, to, cap, flow;
//Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){};
};
struct Dinic{
int n, m, i, s, t;
Edge e;
vector<Edge> edges;
vector<int> G[MAXN];
int d[MAXN], cur[MAXN];
bool vis[MAXN];
void init(int n){
this->n = n;
for (i = ; i <= n; i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from, int to, int cap){
edges.push_back(Edge{ from, to, cap, });
edges.push_back(Edge{ to, from, , });
m = edges.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();
for (i = ; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow){
vis[e.to] = true;
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;
for (int& i = cur[x]; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if (d[x] + == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > ){
e.flow += f;
edges[G[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int MaxFlow(int s, int t, int need){
int flow = ;
this->s = s;
this->t = t;
while (BFS()){
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
if (flow > need) return flow;
}
return flow;
}
};
char a[][];
Dinic p;
const int step[][] = { , , , , -, , , - };
bool vis[][];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%s", a[i] + );
}
int s = , t = * n * m + ;
p.init(t);
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++){
for (int j = ; j <= m; j++){
int u = (i - ) * m + j;
if(a[i][j] == 'W'){
p.AddEdge(s, u, );
for (int k = ; k < ; k++){
int x = i + step[k][];
int y = j + step[k][];
if (x < || y < || x > n || y > m) continue;
if (a[x][y] == 'I'){
p.AddEdge(u, (x - ) * m + y, );
}
}
}
else if (a[i][j] == 'I'){
int v = u + n * m;
p.AddEdge(u, v, );
for (int k = ; k < ; k++){
int x = i + step[k][];
int y = j + step[k][];
if (x < || y < || x > n || y > m) continue;
if (a[x][y] == 'N'){
p.AddEdge(v, (x - ) * m + y, );
if (vis[x][y]) continue;
p.AddEdge((x - ) * m + y, t, );
vis[x][y] = true;
}
}
}
}
}
int ans = p.MaxFlow(s, t, INF);
printf("%d\n", ans);
}
Gym - 100203I I WIN 网络流的更多相关文章
- codeforce Gym 100203I I WIN (网络流)
把'I'拆成容量为1一条边,一个入点一个出点,入点和相邻的'W'连一条容量为1的边,出点和相邻的'N'连一条容量为1,所有的'W'和源点连一条容量为1边,所有的'N'和汇点连一条容量为1的边,表示只能 ...
- 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 ...
- Gym - 101128F Landscaping(网络流)
题意 给你一个\(N×M\)的草地,有高地有低地. 收割机从低地走到高地或者从高地走到低地都要花费用\(A\),你可以花费用\(B\)把一块高地变成低地,或者把一块低地变成高地.收割机每行每列都是必须 ...
- Gym 101128F Landscaping(网络流)题解
题意:n*m的地,从有高地和低地,从高地走到低地或者从低地走到高地花费a,把高地和低地互相改造一次花费b.现在要走遍每一行每一列,问最小花费 思路:超级源点连接所有低地,容量b:所有地向四周建边,容量 ...
- C - Portals Gym - 102006C (网络流最小割)
题目链接:https://cn.vjudge.net/contest/283918#problem/C 题目大意:T个测试数据,然后给你一个字符串,每一个字符串包括‘s’,‘t’,‘o’,‘#’,‘. ...
- Gym 101128F Sheldon Numbers(网络流)
[题目链接] http://codeforces.com/gym/101128/attachments [题目大意] 给出一张地图,分为高地和低地,高低地的交界线上划有红线, 现在要开小车跨过每条红线 ...
- codeforces gym 100357 J (网络流)
题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...
- Landscaping Gym - 101128F (网络流)
Problem F: Landscaping \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 题意是给出一个\(n*m\)的格子,其中一些是低 ...
随机推荐
- 【jQuery04】折叠树
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 虚拟集群LVS及DR模式搭建笔记
LVS(虚拟集群Linux Virtual Server) LVS-NAT:地址转换,数据包来回都要经过NAT转换,所以Director Server(即LVS服务器)将成为系统瓶颈.使用NAT模式将 ...
- 紫书 习题 8-24 UVa 10366 (构造法)
又是一道非常复杂的构造法-- #include<cstdio> #include<algorithm> #define REP(i, a, b) for(int i = (a) ...
- pandas学习笔记 - 文件的写入和输出
# -*- coding: utf-8 -*- """ Created on Tue Aug 28 22:19:26 2018 @author: Dev " ...
- 过Hard题目.周末
# Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Hard . 126 Word ...
- jsoncpp-src-0.5.0.tar.gz 源码错误!!!!
近期在做毕设,使用到了JsonCpp0.5.0版本号的源码! 依照网上的安装配置教程,搭建好环境后就能够使用了! 在这里就不浪费空间去将怎样搭建开发环境了!请大家去google一下就好了! 在解析一个 ...
- Minikube之Win10单机部署Kubernetes(k8s)自动化容器操作的开源平台
Minikube之Win10单机部署 Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作.如果你曾经用过Docker部署容器,那么可以 ...
- rsync同步操作命令
在本地磁盘同步数据 将/home做个备份 # rsync -a --delete /home /backups -a 归档模式,表示以递归方式传输文件, -delete 删除那些接收端还有而发送端已经 ...
- Frame Stacking ZOJ 1083,poj 1128
Frame Stacking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4034 Accepted: 1352 De ...
- 网络通信-ping命令