AtCoder - 2568 Lotus Leaves
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns of squares. We will denote the square at the i-th row from the top and j-th column from the left by (i, j).
Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, S, there is a frog trying to get to another leaf T. The state of square (i, j) is given to you by a character aij, as follows:
.: A square without a leaf.o: A square with a leaf floating on the water.S: A square with the leaf S.T: A square with the leaf T.
The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."
Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.
Constraints
- 2≤H,W≤100
- aij is
.,o,SorT. - There is exactly one
Samong aij. - There is exactly one
Tamong aij.
Input
Input is given from Standard Input in the following format:
H W
a11 … a1W
:
aH1 … aHW
Output
If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print -1 instead.
Sample Input 1
3 3
S.o
.o.
o.T
Sample Output 1
2
Remove the upper-right and lower-left leaves.
Sample Input 2
3 4
S...
.oo.
...T
Sample Output 2
0
Sample Input 3
4 3
.S.
.o.
.o.
.T.
Sample Output 3
-1
Sample Input 4
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Sample Output 4
5
非常神奇的二分图建模!
我们把行和列分别看成二分图两边的一排节点,那么我们的任务其实就是找到一个最小割,使得从S 的行或列 走不到 T的行或列。
所以对于原图中的一片荷叶(i,j) ,我们就在二分图中添加 无向边 行i to 列j,再连 S 到 S行,S列 ; T行,T列到T。容量都是inf。
这样原图中的最小割就是答案了,如果答案>=inf那么无解,说明S,T在同一行或者同一列。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=305,inf=1e8;
#define pb push_back
struct lines{
int to,flow,cap;
}l[maxn*maxn*5];
vector<int> g[maxn];
int cur[maxn],d[maxn],t=-1,S,T;
bool v[maxn]; inline void add(int from,int to,int cap){
l[++t]=(lines){to,0,cap},g[from].pb(t);
l[++t]=(lines){from,0,0},g[to].pb(t);
} inline bool BFS(){
memset(v,0,sizeof(v)),v[S]=1,d[S]=0;
queue<int> q; q.push(S);
int x; lines e; while(!q.empty()){
x=q.front(),q.pop();
for(int i=g[x].size()-1;i>=0;i--){
e=l[g[x][i]];
if(e.flow<e.cap&&!v[e.to]) v[e.to]=1,d[e.to]=d[x]+1,q.push(e.to);
}
} return v[T];
} int dfs(int x,int A){
if(x==T||!A) return A;
int flow=0,f,sz=g[x].size();
for(int &i=cur[x];i<sz;i++){
lines &e=l[g[x][i]];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(e.cap-e.flow,A)))){
A-=f,flow+=f;
e.flow+=f,l[g[x][i]^1].flow-=f;
if(!A) break;
}
} return flow;
} inline int max_flow(){
int an=0;
while(BFS()){
memset(cur,0,sizeof(cur));
an+=dfs(S,inf);
}
return an;
} int n,m;
char ch; int main(){
scanf("%d%d",&n,&m),S=0,T=n+m+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ch=getchar();
while(ch!='.'&&ch!='o'&&ch!='S'&&ch!='T') ch=getchar();
if(ch!='.') add(i,j+n,1),add(j+n,i,1);
if(ch=='S') add(S,i,inf),add(S,j+n,inf);
else if(ch=='T') add(i,T,inf),add(j+n,T,inf);
} int ans=max_flow();
if(ans>=inf) puts("-1");
else printf("%d\n",ans); return 0;
}
AtCoder - 2568 Lotus Leaves的更多相关文章
- AtCoder Regular Contest 074 F - Lotus Leaves
题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...
- AtCoder Regular Contest 074F - Lotus Leaves
$n \leq 300,m \leq 300$,$n*m$的格子里有起点有终点有空地有障碍,人会从起点选一个同行或同列空地跳过去,然后一直这样跳到终点.求至少删掉多少格子使得人跳不到终点. 首先S和T ...
- 【arc077f】AtCoder Regular Contest 074 F - Lotus Leaves
题意 给定一个n*m的池塘,每个格子上可能有叶子. 从一个叶子出发,可以跳到相同行或相同列的叶子. 问至少去掉多少叶子,使得起点不能到达终点. \(n,m<=100\) 解法 很显然的最小割模型 ...
- AtCoder - 2568 最小割
There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns ...
- 【ARC074F】Lotus Leaves 最小割
Description 给你一个n*m网格图,有起点荷叶和终点荷叶,有中转荷叶,其他的格子没东西,一个荷叶可以跳到同一行或者列的另一个荷叶.问最多删掉几个中转荷叶能让起点终点不连通.如果不行输出-1. ...
- agc026F Lotus Leaves
题目链接 题目大意 一个n*m的网格上有一些点,一个点可以跳到与其同一行或同一列的点上.给定起点和终点. 求要使起点不能跳到终点,最少撤走几个点. \(n,m\leq 100\) 解题思路 考虑将能够 ...
- Atcoder Regular-074 Writeup
C - Chocolate Bar 题面 There is a bar of chocolate with a height of H blocks and a width of W blocks. ...
- AtCoder刷题记录
构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...
- 【AtCoder】ARC074
ARC 074 C - Chocolate Bar 直接枚举第一刀横切竖切,然后另一块要求如果横切分成\(H / 2\)竖切分成\(W/2\)即可 #include <bits/stdc++.h ...
随机推荐
- 你是否彻底了解margin属性?
写css,你少不了与margin打交道.你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用padding而不是m ...
- [poj 2274]后缀数组+最长公共子串
题目链接:http://poj.org/problem?id=2774 后缀数组真的太强大了,原本dp是O(nm)的复杂度,在这里只需要O(n+m). 做法:将两个串中间夹一个未出现过的字符接起来,然 ...
- [fzu 2271]不改变任意两点最短路至多删的边数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2271 题目中说每条边的边权都是[1,10]之间的整数,这个条件非常关键!以后一定要好好读题啊…… 做10次循环 ...
- 第九届蓝桥杯C/C++B组题解附代码
1.标题:第几天 2000年的1月1日,是那一年的第1天.那么,2000年的5月4日,是那一年的第几天? 125天 打开日历就ok 2. 标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库 ...
- docker compose,link,Odoo
1.报错: /usr/bin/docker-current: Error response from daemon: driver failed programming external connec ...
- IntelliJ 创建main函数快捷
今天偶然发现了IntelliJ中 创建main函数的快捷键,依次还有for循环,System.out.println(); 在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一 ...
- 获取高德地图api
先到高德开放平台首页按照关键字搜索地址,获取经纬度坐标: http://lbs.amap.com/console/show/picker 高德由坐标获取地址详细信息: http://restapi.a ...
- JavaScript设置获取和设置属性的方法
这篇文章主要介绍了JavaScript设置获取和设置属性的方法,学会使用getAttribute.setAttribute的用法,需要的朋友可以参考下 getAttribute 该方法用来获取元素 ...
- python 学习笔记 aiohttp
asyncio可以实现单进程并发IO操作,如果仅用在客户端,发挥的威力并不大,如果把asyncio用在服务器端,由于http链接就是IO操作, 因此可以用单线程+coroutine实现多客户的高并发支 ...
- Jmeter4.0启动闪退问题解决方案
jmeter:4.0 jdk版本:1.8 在Jmeter.bat的最后添加pause可以让Jmeter启动停止: 添加了pause进行强制停止在启动命令页面,查看到Jmeter报错信息如下: 第一次解 ...