Problem Statement

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 (ij).

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 (ij) 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, S or T.
  • There is exactly one S among aij.
  • There is exactly one T among 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的更多相关文章

  1. AtCoder Regular Contest 074 F - Lotus Leaves

    题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...

  2. AtCoder Regular Contest 074F - Lotus Leaves

    $n \leq 300,m \leq 300$,$n*m$的格子里有起点有终点有空地有障碍,人会从起点选一个同行或同列空地跳过去,然后一直这样跳到终点.求至少删掉多少格子使得人跳不到终点. 首先S和T ...

  3. 【arc077f】AtCoder Regular Contest 074 F - Lotus Leaves

    题意 给定一个n*m的池塘,每个格子上可能有叶子. 从一个叶子出发,可以跳到相同行或相同列的叶子. 问至少去掉多少叶子,使得起点不能到达终点. \(n,m<=100\) 解法 很显然的最小割模型 ...

  4. AtCoder - 2568 最小割

    There is a pond with a rectangular shape. The pond is divided into a grid with H rows and W columns ...

  5. 【ARC074F】Lotus Leaves 最小割

    Description 给你一个n*m网格图,有起点荷叶和终点荷叶,有中转荷叶,其他的格子没东西,一个荷叶可以跳到同一行或者列的另一个荷叶.问最多删掉几个中转荷叶能让起点终点不连通.如果不行输出-1. ...

  6. agc026F Lotus Leaves

    题目链接 题目大意 一个n*m的网格上有一些点,一个点可以跳到与其同一行或同一列的点上.给定起点和终点. 求要使起点不能跳到终点,最少撤走几个点. \(n,m\leq 100\) 解题思路 考虑将能够 ...

  7. 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. ...

  8. AtCoder刷题记录

    构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...

  9. 【AtCoder】ARC074

    ARC 074 C - Chocolate Bar 直接枚举第一刀横切竖切,然后另一块要求如果横切分成\(H / 2\)竖切分成\(W/2\)即可 #include <bits/stdc++.h ...

随机推荐

  1. 测试数据库DG搭建为正式库以后做准备

    Data guard 部署 1.系统准备(备库只需建立数据库软件) 两台操作系统 oracle linux 7 Node1 172.16.70.191 Node2 172.16.70.192 Orac ...

  2. rsync 同步

    1./usr/bin/rsync -vzrtopg --progress  --include "weibo-service-server" --exclude "/*& ...

  3. POJ 2395 Out of Hay (prim)

    题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. B ...

  4. python3 time、random、hashlib模块

    一.时间模块时间的几种形式:时间戳,结构化时间,字符串时间 import time print(time.time()) # 仅仅是当前时间的时间戳 float print(time.localtim ...

  5. PL/SQL 06 函数 function

    --函数 create or replace function  函数名称(参数1 类型1,参数2 类型2,...)  return 数据类型as  变量.常量声明;begin  代码;end; cr ...

  6. 算法题之Climbing Stairs(leetcode 70)

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  7. 无界面运行Jmeter压测脚本

    今天在针对单一接口压测时出现了从未遇到的问题,设好并发量后用调度器控制脚本的开始和结束,但在脚本应该自动结束时间,脚本却停不下来,手动stop报告就会有error率,卡了我很久很久不能解决,网络上也基 ...

  8. rest-framework-@action()装饰器

    路由Routers 使用方法:   在urls.py中定义路由 from rest_framework.routers import DefaultRouter # 定义视图集的路由 router = ...

  9. 【SQL】约束与触发器2

    3.修改约束 3.1给约束命名 按如下格式命名: name ) CONSTRAINT NameIsKey PRIMARY KEY gender ) CONSTRAINT NoAndro CHECK ( ...

  10. hdu 5188(带限制的01背包)

    zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...