Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 16    Accepted Submission(s): 12

Problem Description
Given a maze of size n×m. The rows are numbered 1, 2, · · · , n from top to bottom while the columns are numbered 1, 2, · · · , m from left to right, which means that (1, 1) is the top-left corner and that (n, m) is the bottom-right corner. And for each cell of size 1 × 1, it is either blank or blocked.
There are a robots above the maze. For i-th robot, it is initially positioned exactly above the cell (1, pi), which can be described as (0, pi). And the initial moving direction of the robots are all downward, which can be written as (1, 0) in the vector form.
Also, there are b exits below the maze. For i-th exit, it is positioned exactly below the cell (n, ei), which can be described as (n + 1, ei).
Now, you want to let the robots escape from the maze by reaching one of the exits. However, the robots are only able to go straight along their moving directions and can’t make a turn. So you should set some turning devices on some blank cells in the maze to help the robots make turns.
There are 4 types of turning devices:

  • “NE-devices” : make the robots coming from above go rightward, and make the robots coming from right go upward. Coming from left or below is illegal.
  • “NW-devices” : make the robots coming from above go leftward, and make the robots coming from left go upward. Coming from right or below is illegal.
  • “SE-devices” : make the robots coming from below go rightward, and make the robots coming from right go downward. Coming from left or above is illegal.
  • “SW-devices” : make the robots coming from below go leftward, and make the robots coming from left go downward. Coming from right or above is illegal.

For each cell, the number of turning devices on it can not exceed 1. And collisions between the robots are ignored, which allows multiple robots to visit one same cell even at the same time.
You want to know if there exists some schemes to set turning devices so that all the a robots can reach one of the b exits after making a finite number of moves without passing a blocked cell or passing a turning device illegally or going out of boundary(except the initial position and the exit).
If the answer is yes, print “Yes” in a single line, or print “No” if the answer is no.

 
Input
The first line contains one positive integer T (1 ≤ T ≤ 10), denoting the number of test cases.
For each test case:
The first line contains four positive integers n, m, a, b (1 ≤ n, m ≤ 100, 1 ≤ a, b ≤ m), denoting the number of rows and the number of columns in the maze, the number of robots and the number of exits respectively.
Next n lines each contains a string of length m containing only “0” or “1”, denoting the initial maze, where cell (i, j) is blank if the j-th character in i-th string is “0”, while cell (i, j) is blocked if the j-th character in i-th string is “1”.
The next line contains a integers pi (1 ≤ pi ≤ m), denoting the initial positions (0, pi) of the robots.
The next line contains b integers ei (1 ≤ ei ≤ m), denoting the positions (n + 1, ei) of the exits.
It is guaranteed that all pis are pairwise distinct and that all eis are also pairwise distinct.
 
Output
Output T lines each contains a string “Yes” or “No”, denoting the answer to corresponding test case.
 
Sample Input
2
3 4 2 2
0000
0011
0000
1 4
2 4
3 4 2 2
0000
0011
0000
3 4
2 4
 
Sample Output
Yes
No

Hint

 
Source

题解:

每个格子的水平方向和竖直方向都只能被使用一次,因为两个机器人的路径不可能合并,也不可能迎面相撞。如果一个格子没有放转弯装置,则可以被水平穿过一次,竖直穿过一次。如果一个格子放了转弯装置,则这个格子只能被一个机器人经过一次。所以对于所有非障碍格子,可以拆成水平点和竖直点,每个点限流 1,上下相邻的格子连竖直点(竖直直行),左右相邻的格子连水平点(水平直行),格子内部的水平点和竖直点互相相连(转弯),源连向起点的竖直点,出口的竖直点连向汇,跑最大流,如果最大流 = 机器人个数,则输出 Yes,否则输出 No。
 
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=,M=;
int T,n,m,a,b,h[N],s,t,base;
char g[][];
int head[N],nex[M],w[M],to[M],tot;
inline void ade(int a,int b,int c)
{
to[++tot]=b;
nex[tot]=head[a];
w[tot]=c;
head[a]=tot;
}
inline void add(int a,int b,int c)
{
ade(a,b,c);
ade(b,a,);
}
inline int id(int x,int y){return m*x+y;}
inline int bfs()
{
memset(h,,sizeof h);
h[s]=;
queue<int> q; q.push(s);
while(q.size())
{
int u=q.front(); q.pop();
for(int i=head[u];i;i=nex[i])
{
if(!h[to[i]]&&w[i])
{
h[to[i]]=h[u]+;
q.push(to[i]);
}
}
}
return h[t];
}
int dfs(int x,int f)
{
if(x==t) return f;
int fl=;
for(int i=head[x];i&&f;i=nex[i])
{
if(h[to[i]]==h[x]+&&w[i])
{
int mi=dfs(to[i],min(w[i],f));
w[i]-=mi; w[i^]+=mi; fl+=mi; f-=mi;
}
}
if(!fl) h[x]=-;
return fl;
}
int dinic()
{
int res=;
while(bfs()) res+=dfs(s,inf);
return res;
}
signed main()
{
cin>>T;
while(T--)
{
tot=;
memset(head,,sizeof head);
cin>>n>>m>>a>>b;
base=(n+)*m; t=base*;
for(int i=;i<=n;i++) scanf("%s",g[i]+);
for(int i=;i<=a;i++)
{
int x; scanf("%d",&x); g[][x]='';
add(s,id(,x),); add(id(,x),id(,x),);
}
for(int i=;i<=b;i++)
{
int x; scanf("%d",&x);
g[n+][x]='';
add(id(n+,x),t,inf);
add(id(n,x),id(n+,x),inf);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(g[i][j]=='') continue;
if(i>) add(id(i,j),id(i-,j),);
if(i<n) add(id(i,j),id(i+,j),);
if(j>) add(id(i,j)+base,id(i,j-)+base,);
if(j<m) add(id(i,j)+base,id(i,j+)+base,);
add(id(i,j),id(i,j)+base,);add(id(i,j)+base,id(i,j),);
}
}
puts(dinic()==a?"Yes":"No");
}
return ;
}

2019CCPC秦皇岛 E题 Escape(网络流)的更多相关文章

  1. 2019CCPC秦皇岛D题 Decimal

    Decimal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  2. 2019CCPC秦皇岛I题 Invoker(DP)

    Invoker Time Limit: 15000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  3. 2019CCPC 秦皇岛 E.Escape

    传送门 题意: 给出一个\(n*m\)的迷宫,有\(a\)个入口,\(b\)个出口. 现在有\(a\)个机器人都从入口出发,一开始方向默认为下,你可以选在在一些格子上面放置一个转向器,转向器有四种: ...

  4. 2019-ccpc秦皇岛现场赛

    https://www.cnblogs.com/31415926535x/p/11625462.html 昨天和队友模拟了下今年秦皇岛的区域赛,,,(我全程在演 题目链接 D - Decimal 签到 ...

  5. 2017 CCPC秦皇岛 L题 One Dimensions Dave

    BaoBao is trapped in a one-dimensional maze consisting of  grids arranged in a row! The grids are nu ...

  6. 2019CCPC秦皇岛自我反省&部分题解

    练了一年半了,第一次打CCPC,险些把队友坑了打铁,最后也是3题危险捡了块铜. 非常水的点双连通,我居然不相信自己去相信板子,唉,结果整来整去,本来半个小时能出的题,整到了3个小时,大失误呀,不然就可 ...

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

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

  8. hdu 3572 Escape 网络流

    题目链接 给一个n*m的图, 里面有一些点, '.'代表空地, '#'代表墙, 不可以走, '@'代表大门, 可以有多个, 'X'代表人, 问所有人都走出大门需要的最短时间, 每一时刻一个格子只能有一 ...

  9. POJ 2455 网络流 基础题 二分+网络流 dicnic 以及 sap算法

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8189   Accepted: ...

随机推荐

  1. mariadb数据类型

    MariaDB 数据类型: MariaDB数据类型可以分为 数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行,尽量使用范围小的,而不用大的 常用的数据类型: a. 整数:int, bit ...

  2. jsp页面不乱码,外部引用的js弹出对话框乱码

    今天在做一个课程设计的时候,写到一个界面注册,在用js判断数据的正确性时,碰到了一个js弹出框的乱码问题.在网上找寻了很久,也找了很多博客看,但是发现怎么样都不能解决我的问题,下面给出几个比较经典的解 ...

  3. 使用Bootstrap制作简单的旅游主页

    页面效果 代码: 需要导入bootstrapt文件,解压至项目中. 下载地址:https://v3.bootcss.com/getting-started/#download <!DOCTYPE ...

  4. go-micro+php+consul简单的微服实现

    首先我们用go-micro构建一个服务.(关于go-micro的使用可以参照官方实例或者文档) //新建一个微服务 micro new --type "srv" user-srv ...

  5. Linux运维利器之ClusterShell

    一.简介 实验室机房有大概百台的服务器需要管理,加上需要搭建Hadoop以及Spark集群等,因此,一个轻量级的集群管理软件就显得非常有必要了.经过一段时间的了解以及尝试,最终选择了clustersh ...

  6. Pashmak and Graph(dp + 贪心)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给一个带权有向图, 找出其中最长上升路的长度. 题解:先按权值对所有边排序, 然后依次 u -& ...

  7. img标签不能直接作为body的子元素

    前几天在一本教材上看到关于HTML标签嵌套规则一节的时候,看到这么一句话,“把图像作为body元素的子元素直接插入到页面中,这样是不妥的,一是结构嵌套有误,二是图像控制不方便.”后面还给了一段代码演示 ...

  8. vue—自定义指令

    今日分享—自定义指令 需要学习的点: modifiers属性的具体实例就是v-on:click.stop=”handClick” 一样,为指令添加一个修饰符. 全局指令:新建一个newDir.js i ...

  9. PHP中16个高危函数

    php中内置了许许多多的函数,在它们的帮助下可以使我们更加快速的进行开发和维护,但是这个函数中依然有许多的函数伴有高风险的,比如说一下的16个函数不到万不得已不尽量不要使用,因为许多“高手”可以通过这 ...

  10. php使用QueryList轻松采集JavaScript动态渲染页面

    QueryList使用jQuery的方式来做采集,拥有丰富的插件. 下面来演示QueryList使用PhantomJS插件抓取JS动态创建的页面内容. 安装 使用Composer安装: 安装Query ...