[CCPC2019秦皇岛] E. Escape
[CCPC2019秦皇岛E] Escape
Link
https://codeforces.com/gym/102361/problem/E
Solution
观察到性质若干然后建图跑最大流即可。
我的 ISAP 被卡了,换成 Dinic 却过了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int inf = 1e+9;
#define reset(x) memset(x,0,sizeof x)
int dis[maxn], ans, cnt = 1, s, t, pre[maxn * 10], nxt[maxn * 10], h[maxn], v[maxn * 10];
std::queue<int> q;
void make(int x, int y, int z) {
pre[++cnt] = y, nxt[cnt] = h[x], h[x] = cnt, v[cnt] = z;
pre[++cnt] = x, nxt[cnt] = h[y], h[y] = cnt;
}
bool bfs() {
memset(dis, 0, sizeof dis);
q.push(s), dis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = h[x]; i; i = nxt[i])
if (!dis[pre[i]] && v[i])
dis[pre[i]] = dis[x] + 1, q.push(pre[i]);
}
return dis[t];
}
int dfs(int x, int flow) {
if (x == t || !flow)
return flow;
int f = flow;
for (int i = h[x]; i; i = nxt[i])
if (v[i] && dis[pre[i]] > dis[x]) {
int y = dfs(pre[i], min(v[i], f));
f -= y, v[i] -= y, v[i ^ 1] += y;
if (!f)
return flow;
}
if (f == flow)
dis[x] = -1;
return flow - f;
}
int T,n,m,a,b,p[105],e[105];
char c[105][105];
int calch(int i,int j)
{
return 3+((i-1)*m+j-1)*2;
}
int calcv(int i,int j)
{
return 4+((i-1)*m+j-1)*2;
}
int calcx(int i,int j)
{
return 3+2*n*m+((i-1)*m+j-1);
}
int calcy(int i,int j)
{
return 3+3*n*m+((i-1)*m+j-1);
}
signed main()
{
ios::sync_with_stdio(false);
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&a,&b);
reset(dis); reset(pre); reset(nxt); reset(h); reset(v); cnt=1;
//cout<<calch(n,m)<<" "<<calcv(n,m)<<" "<<calcx(n,m)<<" "<<calcy(n,m)<<endl;
for(int i=1;i<=n;i++) scanf("%s",c[i]+1);
for(int i=1;i<=a;i++) scanf("%d",&p[i]);
for(int i=1;i<=b;i++) scanf("%d",&e[i]);
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++)
if(c[i][j]=='0' && c[i+1][j]=='0')
make(calcv(i,j),calcv(i+1,j),1),
make(calcv(i+1,j),calcv(i,j),1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++)
if(c[i][j]=='0' && c[i][j+1]=='0')
make(calch(i,j),calch(i,j+1),1),
make(calch(i,j+1),calch(i,j),1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(c[i][j]=='0')
make(calch(i,j),calcv(i,j),1),
make(calcv(i,j),calch(i,j),1);
for(int i=1;i<=a;i++) if(c[1][p[i]]=='0') make(1,calcv(1,p[i]),1);
for(int i=1;i<=b;i++) if(c[n][e[i]]=='0') make(calcv(n,e[i]),2,1);
s=1;t=2;
ans = 0;
for (; bfs(); ans += dfs(s, inf));
if(ans == a) printf("Yes\n");
else printf("No\n");
}
}
[CCPC2019秦皇岛] E. Escape的更多相关文章
- [CCPC2019秦皇岛] F. Forest Program
[CCPC2019秦皇岛 F] Link https://codeforces.com/gym/102361/problem/F Description 给定一个仙人掌,删去一些边可以让它变成一个森林 ...
- 2019CCPC 秦皇岛 E.Escape
传送门 题意: 给出一个\(n*m\)的迷宫,有\(a\)个入口,\(b\)个出口. 现在有\(a\)个机器人都从入口出发,一开始方向默认为下,你可以选在在一些格子上面放置一个转向器,转向器有四种: ...
- Codeforces Gym 102361A Angle Beats CCPC2019秦皇岛A题 题解
题目链接:https://codeforces.com/gym/102361/problem/A 题意:给定二维平面上的\(n\)个点,\(q\)次询问,每次加入一个点,询问平面上有几个包含该点的直角 ...
- 2019CCPC秦皇岛 E题 Escape(网络流)
Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 2019-ccpc秦皇岛现场赛
https://www.cnblogs.com/31415926535x/p/11625462.html 昨天和队友模拟了下今年秦皇岛的区域赛,,,(我全程在演 题目链接 D - Decimal 签到 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- 简单明了区分escape、encodeURI和encodeURIComponent
一.前言 讲这3个方法区别的文章太多了,但是大部分写的都很绕.本文试图从实践角度去讲这3个方法. 二.escape和它们不是同一类 简单来说,escape是对字符串(string)进行编码(而另外两种 ...
- c#模拟js escape方法
public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...
- 【BZOJ-1340】Escape逃跑问题 最小割
1340: [Baltic2007]Escape逃跑问题 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 264 Solved: 121[Submit] ...
随机推荐
- 大数据才是未来,Oracle、SQL Server成昨日黄花?
1. 引子**** 有人在某个专注SQL的公众号留言如下: 这个留言触碰到一个非常敏感的问题:搞关系型数据库还有前途吗?现在都2020年了,区块链正火热,AI人才已经"过剩",大数 ...
- Linux进程间通信-管道深入理解(转)
原文地址:https://www.linuxidc.com/Linux/2018-04/151680.htm Linux进程通信系列文章将详细介绍各种通信方式的机制和区别 1.进程间通信 每个进程各自 ...
- C# compare different Encoding pattern between UTF8 and UTF32 based on Md5
using System; using System.Text; using System.IO; using System.Security.Cryptography; static void Ma ...
- 修改Ubuntu的apt-get源为国内镜像源的方法
1.原文件备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 2.编辑源列表文件 sudo vim /etc/apt/sources. ...
- Spark学习之路 (二十)SparkSQL的元数据[转]
概述 SparkSQL 的元数据的状态有两种: 1.in_memory,用完了元数据也就丢了 2.hive , 通过hive去保存的,也就是说,hive的元数据存在哪儿,它的元数据也就存在哪儿. 换句 ...
- 查看appium参数
首先打开appium server并运行,然后将手机与电脑相连,然后在python中写代码,但是代码需要出入appium参数,这些参数怎么查询呢? 1.创建appium参数 { "platf ...
- commons-dbutils实现增删改查(spring新注解)
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- 畅通工程 HDU - 1232 并查集板子题
#include<iostream> #include<cstring> using namespace std; ; int p[N]; int find(int x) { ...
- PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 8,DA=6,则 A 的“6 部分”PA 是 66,因为 A 中有 ...
- 如果linux开机没有ip怎么办
1.vim编辑网卡配置文件,修改如下参数 [root@s25linux tmp]# cd /etc/sysconfig/network-scripts/vim修改此文件,找到如下参数,改为yesONB ...