QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211671#5472. Secure the Top Secretbulijiojiodibuliduo#AC ✓1245ms10936kbC++179.8kb2023-10-12 20:12:242023-10-12 20:12:25

Judging History

你现在查看的是最新测评结果

  • [2023-10-12 20:12:25]
  • 评测
  • 测评结果:AC
  • 用时:1245ms
  • 内存:10936kb
  • [2023-10-12 20:12:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

template <
  typename CapType, typename TotalCapType, 
  typename CostType, typename TotalCostType
>
class CostScaling {
private:
  static const int alpha = 8; // eps <- max(1, eps / alpha)

  using cap_t = CapType;
  using tcap_t = TotalCapType;
  using cost_t = CostType; // > max{|C|} * (2 * |V|)
  using tcost_t = TotalCostType;
  static constexpr cost_t Inf = (tcap_t(1) << (sizeof(tcap_t) * 8 - 2)) - 1;

  struct InputEdge { int from, to; cap_t b, c; cost_t cost; };
  struct Edge { int to, rev; cap_t cap; cost_t cost; };

  class Dinic {
  public:
    Dinic(int N, const vector<int>& ofs, vector<Edge>& edges, 
        vector<tcap_t>& capacity) 
      : N(N), ofs(ofs), edges(edges), capacity(capacity), last(N) {}

    bool succeeded() {
      // s -> u: capacity[u]
      // u -> t: capacity[u + N]
      tcap_t f = 0;
      for (int u = 0; u < N; ++u) f += capacity[u];
      vector<int> que(N);
      while (f) {
        dist.assign(N, -1);
        int qh = 0, qt = 0, lv = N;
        for (int u = 0; u < N; ++u) if (capacity[u] > 0) que[qt++] = u, dist[u] = 0;
        for (; qh < qt; ) {
          int u = que[qh++];
          if (lv == N && capacity[u + N] > 0) lv = dist[u];
          if (dist[u] > lv) break;
          for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
            int v = edges[ei].to;
            if (edges[ei].cap > 0 && dist[v] == -1) {
              que[qt++] = v, dist[v] = dist[u] + 1;
            }
          }
        }
        if (lv == N) break;
        for (int u = 0; u < N; ++u) last[u] = ofs[u];
        for (int u = 0; u < N; ++u) if (capacity[u] > 0) {
          auto df = block_flow(u, capacity[u]);
          f -= df, capacity[u] -= df;
        }
      }
      return f == 0;
    }

  private:
    tcap_t block_flow(int u, tcap_t f) {
      tcap_t ret = 0;
      if (capacity[u + N] > 0) {
        tcap_t df = min(f, capacity[u + N]);
        capacity[u + N] -= df;
        return df;
      }
      for (auto& ei = last[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei]; int v = e.to;
        if (e.cap == 0 || dist[v] <= dist[u]) continue;
        cap_t df = block_flow(v, min<cap_t>(e.cap, f));
        if (df == 0) continue;
        e.cap -= df, edges[e.rev].cap += df;
        f -= df, ret += df;
        if (f == 0) break;
      }
      return ret;
    }

    int N;
    const vector<int>& ofs;
    vector<Edge>& edges;
    vector<tcap_t>& capacity;
    vector<int> last, dist;
  };

public:
  CostScaling(int N, int M=0) : N(N), capacity(2 * N) {
    if (M > 0) in.reserve(M);
  }

  void add_directed_edge(int u, int v, cap_t b, cap_t c, cost_t cost) {
    if (b > 0) capacity[v] += b, capacity[u + N] += b;
    else capacity[u] += -b, capacity[v + N] += -b;
    in.push_back({u, v, b, c, cost});
  }

  pair<bool, tcost_t> minimum_cost_circulation() {
    construct();
    if (!has_feasible_circulation()) return {false, 0};

    const int cost_multiplier = 2 << (31-__builtin_clz(N)); // should be > |V|
    assert(cost_multiplier>=N);
    cost_t eps = 0;
    for (auto& e : edges) e.cost *= cost_multiplier, eps = max(eps, e.cost);
    
    while (eps > 1) refine(eps = max<cost_t>(1, eps / alpha));

    tcost_t ret = initial_cost;
    for (auto& e : edges) ret -= (e.cost / cost_multiplier) * e.cap;
    return {true, ret / 2};
  }

private:
  void refine(const cost_t eps) {
    auto cost_p = [&] (int u, const Edge& e) {
      return e.cost + potential[u] - potential[e.to];
    };
    for (int u = 0; u < N; ++u) for (int i = ofs[u]; i < ofs[u + 1]; ++i) {
      auto& e = edges[i];
      if (cost_p(u, e) < 0) edges[e.rev].cap += e.cap, e.cap = 0;
    }
    vector<tcap_t> excess(initial_excess);
    for (auto& e : edges) excess[e.to] -= e.cap;

    vector<int> stack; stack.reserve(N);
    for (int u = 0; u < N; ++u) if (excess[u] > 0) stack.push_back(u);

    auto residue = [&] (const Edge& e) -> cap_t { return e.cap; };
    auto push = [&] (int u, Edge& e, cap_t df) {
      e.cap -= df; edges[e.rev].cap += df;
      excess[e.to] += df; excess[u] -= df;
      if (excess[e.to] > 0 && excess[e.to] <= df) {
        stack.push_back(e.to);
      }
    };
    auto relabel = [&] (int u, cost_t delta) {
      potential[u] -= delta + eps;
    };
    auto relabel_in_advance = [&] (int u) {
      if (excess[u] != 0) return false;
      auto delta = Inf;
      for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei];
        if (residue(e) == 0) continue;
        if (cost_p(u, e) < 0) return false;
        else delta = min<tcost_t>(delta, cost_p(u, e));
      }
      relabel(u, delta);
      return true;
    };
    auto discharge = [&] (int u) {
      auto delta = Inf;
      for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei];
        if (residue(e) == 0) continue;
        if (cost_p(u, e) < 0) {
          if (relabel_in_advance(e.to)) {
            --ei; continue; // modify ei (!)
          }
          cap_t df = min<tcap_t>(excess[u], residue(e));
          push(u, e, df);
          if (!excess[u]) return;
        } else delta = min<tcost_t>(delta, cost_p(u, e));
      }
      relabel(u, delta);
      stack.push_back(u);
    };
    while (!stack.empty()) {
      auto u = stack.back(); stack.pop_back();
      discharge(u);
    }
  }

  void construct() {
    ofs.assign(N + 1, 0);
    edges.resize(2 * in.size());
    initial_excess.assign(N, 0);
    initial_cost = 0;
    potential.assign(N, 0);
    for (auto& e : in) ofs[e.from + 1]++, ofs[e.to + 1]++;
    for (int i = 1; i <= N; ++i) ofs[i] += ofs[i - 1];
    for (auto& e : in) {
      initial_excess[e.to] += e.c;
      initial_excess[e.from] += -e.b;
      initial_cost += tcost_t(e.cost) * (e.c + e.b);
      edges[ofs[e.from]++] = {e.to, ofs[e.to], e.c - e.b, e.cost};
      edges[ofs[e.to]++] = {e.from, ofs[e.from] - 1, 0, -e.cost};
    }
    for (int i = N; i > 0; --i) ofs[i] = ofs[i - 1];
    ofs[0] = 0;
  }

  bool has_feasible_circulation() {
    return Dinic(N, ofs, edges, capacity).succeeded();
  }

private:
  int N; 
  vector<InputEdge> in;
  vector<tcap_t> capacity;

  vector<int> ofs;
  vector<Edge> edges;

  tcost_t initial_cost;
  vector<tcap_t> initial_excess;
  vector<tcost_t> potential;
};
// cap, total_cap, cost * (2 * |V|), total_cost
using MCC = CostScaling<int64_t, int64_t, int64_t, int64_t>;
// using MCC = CostScaling<int, int, int, int>;
const int N=110;
int n,m,k;
char s[N][N];
bool hor[N][N],ver[N][N];
const ll inf=1000000000;
PII dir[4]={{-1,0},{0,1},{1,0},{0,-1}};

bool inside(int x,int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,0,n) scanf("%s",s[i]);
	scanf("%d",&k);
	rep(i,0,k) {
		int x,y;
		static char t[11];
		scanf("%d%d%s",&x,&y,t);
		--x; --y;
		if (t[0]=='b') ver[x][y]=1;
		else hor[x][y]=1;
	}
	PII sp,tp,up;
	rep(i,0,n) rep(j,0,m) {
		if (s[i][j]=='S') sp=mp(i,j);
		if (s[i][j]=='T') tp=mp(i,j);
		if (s[i][j]=='U') up=mp(i,j);
	}
	vector<PII> bor;
	PII pos=sp;
	int d=0;
	while (inside(sp.fi+dir[d].fi,sp.se+dir[d].se)) ++d;
	int sd=-1;
	while (1) {
		while (!inside(pos.fi+dir[d].fi,pos.se+dir[d].se)||s[pos.fi+dir[d].fi][pos.se+dir[d].se]=='#') d=(d+1)%4;
		if (pos==sp&&d==sd) break;
		if (pos==sp&&sd==-1) sd=d;
		bor.pb(pos);
		pos.fi+=dir[d].fi; pos.se+=dir[d].se;
		d=(d+3)%4;
	}
	//for (auto [x,y]:bor) printf("%d %d\n",x,y);
	if (find(all(bor),tp)==bor.end()) {
		puts("-1");
		return 0;
	}
	if (find(all(bor),up)==bor.end()) {
		puts("0");
		return 0;
	}
	auto t1=find(all(bor),tp)-bor.begin();
	auto t2=find(all(bor),up)-bor.begin();
	if (t1>t2) reverse(bor.begin()+1,bor.end());
	rep(i,0,SZ(bor)-1) {
		auto [x1,y1]=bor[i];
		auto [x2,y2]=bor[i+1];
		if (bor[i]==up) {
			puts("-1");
			return 0;
		}
		//printf("%d %d %d %d\n",x1,y1,x2,y2);
		if (x1==x2) hor[x1][min(y1,y2)]=0;
		if (y1==y2) ver[min(x1,x2)][y1]=0;
		if (bor[i+1]==tp) break;
	}
	int S=sp.fi*m+sp.se,T=up.fi*m+up.se;
	MCC mcc(n*m);

	rep(i,0,n) rep(j,0,m) {
		if (i+1<n&&s[i][j]!='#'&&s[i+1][j]!='#') {
			int u=i*m+j,v=i*m+j+m;
			if (!ver[i][j]) {
				mcc.add_directed_edge(u,v,0,inf,0);
				mcc.add_directed_edge(v,u,0,inf,0);
			} else {
				mcc.add_directed_edge(u,v,0,inf,1);
				mcc.add_directed_edge(v,u,0,inf,1);
				mcc.add_directed_edge(u,v,0,1,0);
				mcc.add_directed_edge(v,u,0,1,0);
			}
		}
		if (j+1<m&&s[i][j]!='#'&&s[i][j+1]!='#') {
			int u=i*m+j,v=i*m+j+1;
			if (!hor[i][j]) {
				mcc.add_directed_edge(u,v,0,inf,0);
				mcc.add_directed_edge(v,u,0,inf,0);
			} else {
				mcc.add_directed_edge(u,v,0,inf,1);
				mcc.add_directed_edge(v,u,0,inf,1);
				mcc.add_directed_edge(u,v,0,1,0);
				mcc.add_directed_edge(v,u,0,1,0);
			}
		}
	}
	mcc.add_directed_edge(S,T,0,inf,-2);
	auto ret=mcc.minimum_cost_circulation();
	ret.se=-ret.se;
	//printf("%lld\n",d.se);
	if (!ret.fi) puts("-1"); else {
		if (ret.se>=100000) puts("-1");
		else printf("%lld\n",ret.se);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3836kb

input:

3 3
S..
#..
U.T
7
1 2 b
1 3 b
2 2 b
2 2 r
2 3 b
3 1 r
3 2 r

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3660kb

input:

2 2
ST
.U
4
1 1 r
1 1 b
1 2 b
2 1 r

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3732kb

input:

7 10
U.........
..........
###.......
..........
.......###
..........
S........T
18
4 4 r
5 4 r
6 7 r
7 7 r
3 4 b
3 5 b
3 6 b
3 7 b
3 8 b
3 9 b
3 10 b
5 1 b
5 2 b
5 3 b
5 4 b
5 5 b
5 6 b
5 7 b

output:

14

result:

ok single line: '14'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

2 5
.T.#S
....U
10
1 3 b
1 1 r
1 1 b
2 1 r
1 2 b
1 5 b
2 2 r
1 2 r
2 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

5 5
U.S..
.....
.....
.....
.T...
12
2 4 b
4 1 b
2 2 r
1 5 b
2 2 b
4 3 b
5 3 r
1 2 b
3 2 r
2 1 r
3 3 r
2 4 r

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3680kb

input:

5 4
....
...U
....
S#..
.#T.
12
3 4 b
2 1 b
4 3 r
2 2 b
4 3 b
3 3 r
2 3 r
1 1 b
2 2 r
4 4 b
3 1 b
1 3 r

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

3 3
UST
###
.#.
2
1 1 r
1 2 r

output:

-1

result:

ok single line: '-1'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

4 2
U.
..
.T
S.
8
1 1 b
3 1 r
4 1 r
1 1 r
2 2 b
3 2 b
3 1 b
2 1 r

output:

5

result:

ok single line: '5'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

2 2
S.
UT
4
1 1 b
1 2 b
2 1 r
1 1 r

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

3 4
#U.T
#...
.S#.
3
1 2 r
2 2 b
3 1 r

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

3 5
.U.S.
.#.##
#.T#.
7
1 3 r
1 2 r
1 4 r
1 1 b
1 3 b
2 3 b
3 2 r

output:

-1

result:

ok single line: '-1'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3704kb

input:

4 4
.T.S
#.#.
....
U...
6
3 1 b
1 4 b
2 4 b
3 2 b
4 1 r
3 2 r

output:

-1

result:

ok single line: '-1'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

5 5
.##T.
##.#.
.#...
##.##
#SU..
6
2 3 b
3 3 r
4 3 b
1 5 b
3 4 r
5 4 r

output:

-1

result:

ok single line: '-1'

Test #14:

score: 0
Accepted
time: 449ms
memory: 6512kb

input:

97 97
S...............................................................................................T
.................................................................................................
.................................................................................................
...

output:

130

result:

ok single line: '130'

Test #15:

score: 0
Accepted
time: 496ms
memory: 6596kb

input:

100 94
S............................................................................................T
..............................................................................................
..............................................................................................
...........

output:

126

result:

ok single line: '126'

Test #16:

score: 0
Accepted
time: 428ms
memory: 6596kb

input:

95 92
S..........................................................................................T
............................................................................................
............................................................................................
..................

output:

124

result:

ok single line: '124'

Test #17:

score: 0
Accepted
time: 426ms
memory: 6460kb

input:

92 92
S..........................................................................................T
............................................................................................
............................................................................................
..................

output:

124

result:

ok single line: '124'

Test #18:

score: 0
Accepted
time: 506ms
memory: 6704kb

input:

96 100
S..................................................................................................T
....................................................................................................
..............................................................................................

output:

134

result:

ok single line: '134'

Test #19:

score: 0
Accepted
time: 584ms
memory: 6320kb

input:

92 90
S........................................................................................T
..........................................................................................
..........................................................................................
........................

output:

120

result:

ok single line: '120'

Test #20:

score: 0
Accepted
time: 577ms
memory: 6572kb

input:

96 90
S........................................................................................T
..........................................................................................
..........................................................................................
........................

output:

120

result:

ok single line: '120'

Test #21:

score: 0
Accepted
time: 623ms
memory: 6416kb

input:

92 94
S............................................................................................T
..............................................................................................
..............................................................................................
............

output:

126

result:

ok single line: '126'

Test #22:

score: 0
Accepted
time: 596ms
memory: 6868kb

input:

99 94
S............................................................................................T
..............................................................................................
..............................................................................................
............

output:

126

result:

ok single line: '126'

Test #23:

score: 0
Accepted
time: 692ms
memory: 6440kb

input:

93 91
S.........................................................................................T
...........................................................................................
...........................................................................................
.....................

output:

122

result:

ok single line: '122'

Test #24:

score: 0
Accepted
time: 100ms
memory: 5608kb

input:

50 83
........................#.............................#........#......#.....#......
...........................................#........#................#........#....
....................#.....#........................................................
................................#............

output:

22

result:

ok single line: '22'

Test #25:

score: 0
Accepted
time: 202ms
memory: 4884kb

input:

58 98
.........#.....#...U............#..............#.......#....................##...........#........
.........#........#.....#...#..#........#.#.........#..................#.#........................
.#.##.......#........#......#..#.....#.........#..#.........#...##.........###..##......#..........

output:

-1

result:

ok single line: '-1'

Test #26:

score: 0
Accepted
time: 107ms
memory: 4932kb

input:

80 48
..##.#.........#...#.....#............#.#.#.#...
....#........#.......#...#.##.#.##....#.##.#...#
#......##.#..#.......#.....##..#..#.......##.#.#
.##.#...#.#.#.#.....###...#......#............##
.....#.#.....#.#..##.......##..#...#.#..#..##...
.......#....#.#...##.##.##....#...#......#..####
...

output:

2

result:

ok single line: '2'

Test #27:

score: 0
Accepted
time: 23ms
memory: 4392kb

input:

40 49
#.....................#..................#.......
..................#......#....#..#....#.#.......U
......#.................................#........
.................................................
#.......#.......#..........#.....................
....#.......#.......#............#.............

output:

9

result:

ok single line: '9'

Test #28:

score: 0
Accepted
time: 12ms
memory: 3744kb

input:

25 39
.......................#...............
...................................#...
.......................................
##.......................#.............
..................#.........#..........
..#...................................U
...............................#.......
.................

output:

-1

result:

ok single line: '-1'

Test #29:

score: 0
Accepted
time: 8ms
memory: 3800kb

input:

17 47
..#.....#...#...#........................T.#...
.........................#.........#......#....
.....#..........##..#.......#...#........#.....
......#.......................#................
##...#........#.......##.#.#.......#.##........
...............................................
#..#.....

output:

-1

result:

ok single line: '-1'

Test #30:

score: 0
Accepted
time: 5ms
memory: 3840kb

input:

18 66
...#..#...#..#.#...#U#.#....##...#.#.#.......#.#..........#......#
....##...#...##..##.....#..#.##.#..##...........#....#.....#...#.#
#...##..........##.....#........#...##.#.##............#.#...#.#..
#.#..#..#........#..#....#...#..#.....##..#..#..............#....#
.........#...##.#..#.#.###...

output:

4

result:

ok single line: '4'

Test #31:

score: 0
Accepted
time: 149ms
memory: 4852kb

input:

69 81
.....#..........#.##..U....#..#.S#....#..###..##...........#....##..####.#T.##.##
##........##...##.#.#.##...#.###.#...#....##..#...#..###.....#.................#.
.......##...###..#....#..###.....#..##...#....#..#..#...#.#...#.#....#...##...#.#
#...#...........##..###.#.#.##.#...#.#....#..#.#...

output:

2

result:

ok single line: '2'

Test #32:

score: 0
Accepted
time: 53ms
memory: 3956kb

input:

87 26
.##....#.U.#.#.#......#...
...#...##..#...#.##...#..T
#..........#...#.....#.#..
##.#..##...##.#..#..#.....
................##.....##.
......###..##.#..#..#.....
...#.....#....##..#.......
#.......###..#.....#......
######..##......#.....#...
#.#.#....#.#.........#..#.
.#.....#..###..#......#....

output:

4

result:

ok single line: '4'

Test #33:

score: 0
Accepted
time: 17ms
memory: 3892kb

input:

19 92
.#....#.#.....#.#.........###..S##.#...#..#.#..#...#..####.......#.......#...#.##.##...##...
.......#.#.#.#..#...#..###..#...#..#....#......##........#.#.........#..#.###....#.....#....
......#...#..#.#...#.....##..##......##.###....#...#....#.#.###..#...#####.##..#....#..#.###
.##...##.#.#..#...

output:

2

result:

ok single line: '2'

Test #34:

score: 0
Accepted
time: 336ms
memory: 5560kb

input:

92 96
...#..##.#..##.......#.....#..#.#S#....#.........##...#.#..#...#.......#..#............#..#..#..
...#.#.#.#....#.##....#....#.#....#...#...#....#..##....#..#....#...#..#....#....##....#..#.....
.#...#..#.........#..........#.##.#..#..#.#..#..#.....#.#....#.#.#..........##.......#.#....##..
......

output:

-1

result:

ok single line: '-1'

Test #35:

score: 0
Accepted
time: 207ms
memory: 6468kb

input:

90 90
##....#........#..#..#.#...#...#.#....#.#....##..#.#......#...#......T.#..####........#.#.
..#...............#.#...#...#......#.......#.#..#.#.......#...#.....##....#..##.....#.....
.###.......##.##.........#.#.#..##.##..#........#.#......#......#.#.#..###..##..#.#.....#.
..##......#.........#...

output:

6

result:

ok single line: '6'

Test #36:

score: 0
Accepted
time: 816ms
memory: 5956kb

input:

98 93
..#...........##..........##.#......#....#.....#.......#..#.#.#.......#..................U.#.
..#........#..#..#...#...##.................#.#........#.......##.#..#..#.....#.......#.###.#
...........###..#####.#..##.......#.#........#.#.....#.......#......#....#..#..#.............
#...#.#........

output:

-1

result:

ok single line: '-1'

Test #37:

score: 0
Accepted
time: 768ms
memory: 6288kb

input:

96 97
#............#.................#....#.#.#...#....#..###..#.#....#.##...#..#...........#..........
..#.........##........#...#.##....##....###...#.###..#.#..............##.#.....................#.
..#..........#.#.#..#...........#...##............#......##...................#......##....#.....
...

output:

-1

result:

ok single line: '-1'

Test #38:

score: 0
Accepted
time: 212ms
memory: 6112kb

input:

91 94
#...........#.#.##.#..#.....#.#........S.##.......##....#..#........#..#...#..#..#........#U#.
...#.....#..#.....##.#..#.##....#...#.##....#....#......##.#.#............##.......#....#....#
#..#...#..#...#.#..###.#...#....#..##....#....##....#.#.##.........#..#...#.#........#.#.#..##
....##......

output:

4

result:

ok single line: '4'

Test #39:

score: 0
Accepted
time: 483ms
memory: 5780kb

input:

94 91
...........#......#...#.....#..###.....#.......#....##.#....#........#......####.......#...
..#...###...##.......#..#....#..#.###.#....#......#.#..####.##...#...#..##..#.#...###..#...
..#.#.##..#...........#.........#.#.......#...........#.###......#..#....#......#...#......
.#.....##..#.....#...

output:

21

result:

ok single line: '21'

Test #40:

score: 0
Accepted
time: 192ms
memory: 5804kb

input:

90 94
##..........#.#.#.....#.....#..#..#......#.#..##..##....##..#.#.#..#.###.#...#.........T.#....
....#.#.....#.#...............##..#..##.##.#..#....##....###.#.#.#...#..###.........#.##....##
...##.............#..#.....#..#..#.......#....#...###....#.#..#...#.#....#...###..####........
#..##...#...

output:

24

result:

ok single line: '24'

Test #41:

score: 0
Accepted
time: 636ms
memory: 7392kb

input:

93 98
.............#.........#.....U..#......................##.........................................
...........#............#...........................................................###...#.......
....................................................................................#..............

output:

-1

result:

ok single line: '-1'

Test #42:

score: 0
Accepted
time: 447ms
memory: 6940kb

input:

96 100
..........#........#..#...#..#.........#....#....#.#.....##......##....##............#.............#
.......#.#..#.#.#..#....................#..............##.....#.........#...#...#.....#.....#.....#.
##.....##.#.###...#...#.#.............###.#.......#.#................#........#........##.....

output:

12

result:

ok single line: '12'

Test #43:

score: 0
Accepted
time: 892ms
memory: 8056kb

input:

100 99
................#..#............T.....#.....#.....#............#.#.....U.#..#.....#..........#.....
.................................#.#...............................................................
............#..........#..#..#.........#...................#............................#.......

output:

100

result:

ok single line: '100'

Test #44:

score: 0
Accepted
time: 1ms
memory: 3708kb

input:

13 22
..#...#U#......#....##
......##.............#
......#....#...#.....#
.........#..#.........
...................#..
......###.........#..#
....#..............#.T
.#..#...#.............
#.....#.....##...#....
.#.........#.##.......
......#..#............
.#.....#...#...#......
..S..#.#.............

output:

0

result:

ok single line: '0'

Test #45:

score: 0
Accepted
time: 1ms
memory: 3676kb

input:

84 35
...#.#.#.......###..#......##.###..
...#....##.#..##......###.#..#.##..
...###.....#..###.#..#.#..#...#...#
#...###.#..#..#....#...#.#.#.....#.
###...##......#.##.#.....#...#.#..#
...#.#...###......#.....#....#....#
#.#..##.##.###...#..###....##.##...
###..#.#........#...#.#.##.#.#.....
...#.....

output:

0

result:

ok single line: '0'

Test #46:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

3 82
#..#.#.....#........#.##........####...#...#..##...#......#..S.##..#..#..#..##..#.
...#..##.............#...#..#....#...............#.#....##....#...................
.#..............U................#...#...##.......#..#...T.#..#......#...#......#.
250
2 73 b
2 19 r
3 11 r
2 13 r
2 35 b
3 64 r
...

output:

0

result:

ok single line: '0'

Test #47:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

85 67
...##....##..#..#....##..##S.##...#....#.#............#............
.......#.#..#....#..#..#.#...##..##.#...#.#.#...#......#.....##..#.
..#.....#.....#.#.......#.....#.......#..#.###..#.........#........
##.#.#.#.#.###.#..........##....#.....#.#..##.#...........#...###.#
...........#.......#.....

output:

0

result:

ok single line: '0'

Test #48:

score: 0
Accepted
time: 1ms
memory: 3688kb

input:

42 30
##.....##.#.#T.#.#.#.#....#.#.
..#.......##...#.#.#...#......
#.........#...#.##....#..#....
.....#..#....#..#...##...####.
#.......###...#..#......#..#..
...#.....#......#..#......#.##
##.#.#.#..#...#.......####....
....#...##.....#....#.#.#.....
........#..#.#......#....#.#..
.#.##.#...###.....

output:

0

result:

ok single line: '0'

Test #49:

score: 0
Accepted
time: 45ms
memory: 5256kb

input:

62 62
..........##...#.#.............#.#..........#.................
...............#.#............#..........................#....
...................#.........................##...............
........#............#..................#................#....
..........................#..#.......##......

output:

8

result:

ok single line: '8'

Test #50:

score: 0
Accepted
time: 246ms
memory: 6400kb

input:

89 74
...#....#.#....#...........#..#T.#....................##.U.............##.
...##.....#......##......................#....#.#..#....#..#........##....
...#.##............#...#.........#........#.#..........##.#..#............
.#.......#.....#.....#..#..............##.#................#............

output:

4

result:

ok single line: '4'

Test #51:

score: 0
Accepted
time: 66ms
memory: 5356kb

input:

100 37
.....................#..#.........#..
.............#........#........#.....
...........#.........................
..##..#.....#.............#..........
............#..................#.....
#..#................##...#...........
..#...........#...#..................
..................#.#.....#...

output:

6

result:

ok single line: '6'

Test #52:

score: 0
Accepted
time: 32ms
memory: 4720kb

input:

44 89
...#.#.#...##.#.#.....#..##..#.#......##.........#..##....#.....##....##.#.........#..#..
##....##.#...........#...#..##.#....#...#.#.#......#........#..#..........#.......##....#
....#..#...#.#......#.#...###.#......#....#.#.#......#...##..............##.#.##...#..#..
.#.....###.#..#...#..#.....

output:

4

result:

ok single line: '4'

Test #53:

score: 0
Accepted
time: 8ms
memory: 3988kb

input:

68 17
##.....#....#....
.#.##...#...#...#
...#....#....##..
..#...#........#.
..#..####.#...##.
.....###.........
.#.........#..#.#
....#.#.#.#.....#
...#.....#.......
#..#.###...####..
..##.##....#.#...
##.....#.#.....#.
....#....#......#
..##.............
.....#.........#.
....#...........#
..##.....

output:

6

result:

ok single line: '6'

Test #54:

score: 0
Accepted
time: 1ms
memory: 3756kb

input:

16 23
..#..##.#U.#...#....#.#
...#...##.##.#...##...#
...#.#..#..##.#..#.#...
...#..#.#.#..#....#...#
##..#..#........#......
.......#.#.#........#..
.#.....#...#..#.#......
........#..###.....###.
.##.......#.###..##.#..
.....#..######......#..
#..#....#....#.#....#.#
....#.#..#....##....##.
.#.#.....

output:

-1

result:

ok single line: '-1'

Test #55:

score: 0
Accepted
time: 29ms
memory: 4276kb

input:

43 67
.............#....#.............................S.#............#...
..........#.................#.............#........................
................................#................................#.
.........................#..........#...........#..................
....#....................

output:

-1

result:

ok single line: '-1'

Test #56:

score: 0
Accepted
time: 15ms
memory: 3864kb

input:

79 22
.#......#..#...#.#...#
............#.........
.###.......#.#..#....#
..#..#...........##...
......#.....###....#.#
#.......#..#........#.
.....#.....#..........
......#.#.#.......#...
....#......##..#....#.
...#...#......##......
.....#...#.#..###.....
.....###.......#......
#...........#..#.....

output:

-1

result:

ok single line: '-1'

Test #57:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

9 18
...#...#....#....#
.........##.......
..#.....#.#..###.U
...#..#..####...#T
.#.###.##.......#.
......#....####.#.
#....#....##.#..##
S............#.#..
###..#....#...#..#
0

output:

-1

result:

ok single line: '-1'

Test #58:

score: 0
Accepted
time: 16ms
memory: 3848kb

input:

40 72
#...#...#......#.#.#....#.##..##.#..##.#####..#.##...###.#.####..##.....
.###.#..#.......#..#..#..#.#.#....##.....#..###.#..#.##..#.#.#....#....#
..#.#..#..#..#....#..####....#.#....##............#..#...##..#.#.#..#..#
####..#.##.....#..#........#.#......#...#..###.......##..#..............
.....

output:

-1

result:

ok single line: '-1'

Test #59:

score: 0
Accepted
time: 766ms
memory: 8328kb

input:

100 100
....................................................................................................
.........#..........................................................................................
.........................................................................................#...

output:

-1

result:

ok single line: '-1'

Test #60:

score: 0
Accepted
time: 400ms
memory: 5948kb

input:

100 100
...........#..........................##...#....#.#..............##.....#.#..........#.......#...#..
.....#.....#...#...........#....##............#.....##................#..##.......###...#...........
..#......##.....#.#.#.....#...#.#.....#..#........................#.#.#........#.#..###..#...

output:

-1

result:

ok single line: '-1'

Test #61:

score: 0
Accepted
time: 1245ms
memory: 7284kb

input:

100 100
................##.....#........................................#...##.......#...#..S...##.......U..
...#.....#..................................#.............###......................#................
.........#....#......#.....................................#......#.............#............

output:

-1

result:

ok single line: '-1'

Test #62:

score: 0
Accepted
time: 421ms
memory: 7568kb

input:

100 100
..........................#........#..............#.........##.#.#....#....#...#....................
.......................#.................#..##..................#.....................#..#.#........
...........#....................#....................#....#...................#..............

output:

17

result:

ok single line: '17'

Test #63:

score: 0
Accepted
time: 427ms
memory: 8636kb

input:

100 100
.......................................U............................................................
............................................................................##......................
.............................................................#...............................

output:

42

result:

ok single line: '42'

Test #64:

score: 0
Accepted
time: 316ms
memory: 9696kb

input:

100 100
....................................................................................................
....................................................................................................
.............................................................................................

output:

10

result:

ok single line: '10'

Test #65:

score: 0
Accepted
time: 305ms
memory: 9960kb

input:

100 100
#........................................#.......................#..................................
..............................................#......#..................#......................#....
....#...........#................#......#....................................................

output:

10

result:

ok single line: '10'

Test #66:

score: 0
Accepted
time: 242ms
memory: 7848kb

input:

100 100
...##.##......##.........#....###...#.#..#.#..##..###..#..#.#..#...#.#.........#.#.#....#.#..#.#...T
...#.#..##.#..#.#.................#..............#.#.....#........##....#....#..........#..#.#......
#.#.....#...#...#..##...................#.#..#..##.#......#..#........#.#....#...#...#.......

output:

3

result:

ok single line: '3'

Test #67:

score: 0
Accepted
time: 290ms
memory: 9572kb

input:

100 100
.............................#.......................#..............................................
...............................................................................#....................
......#...###.......................................................##............#..........

output:

6

result:

ok single line: '6'

Test #68:

score: 0
Accepted
time: 327ms
memory: 10936kb

input:

100 100
.......#......................#.....#.............#.....T.......#.............#.....................
.....................................#..............................#...................#.......##..
................................#....#........................#..............................

output:

7

result:

ok single line: '7'

Test #69:

score: 0
Accepted
time: 602ms
memory: 8292kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

9902

result:

ok single line: '9902'

Test #70:

score: 0
Accepted
time: 465ms
memory: 8240kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

9506

result:

ok single line: '9506'

Test #71:

score: 0
Accepted
time: 458ms
memory: 8332kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

2280

result:

ok single line: '2280'

Test #72:

score: 0
Accepted
time: 416ms
memory: 8484kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

1470

result:

ok single line: '1470'

Test #73:

score: 0
Accepted
time: 538ms
memory: 8452kb

input:

100 100
S..................................................................................................T
....................................................................................................
.............................................................................................

output:

102

result:

ok single line: '102'

Test #74:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

2 3
STU
###
2
1 1 r
1 2 r

output:

-1

result:

ok single line: '-1'

Test #75:

score: 0
Accepted
time: 1ms
memory: 3692kb

input:

2 4
ST.U
####
3
1 1 r
1 2 r
1 3 r

output:

2

result:

ok single line: '2'

Test #76:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

3 5
S.U##
##.##
..T..
4
1 1 r
1 2 r
1 3 b
2 3 b

output:

-1

result:

ok single line: '-1'

Test #77:

score: 0
Accepted
time: 1ms
memory: 3744kb

input:

10 10
......S...
..........
##........
.....#....
.....#..##
.........T
U.........
..........
....#.....
....#.....
30
6 9 r
3 7 r
4 8 b
5 8 b
2 6 b
3 3 b
7 5 b
4 1 b
4 3 b
6 7 b
7 6 r
6 9 b
3 7 b
6 6 r
4 4 r
4 5 b
8 5 r
4 7 b
3 4 r
5 7 b
6 8 b
7 6 b
4 4 b
4 2 b
2 5 b
6 5 r
2 7 b
3 4 b
7 5 r
8 4 r

output:

21

result:

ok single line: '21'

Test #78:

score: 0
Accepted
time: 1ms
memory: 3748kb

input:

10 10
..........
..........
..........
..........
..........
.....#....
##....#...
U...#..#..
####......
ST........
25
8 9 b
7 9 r
9 8 r
6 4 r
10 1 r
3 5 b
6 9 b
9 6 b
7 9 b
4 4 r
9 8 b
4 6 r
9 5 r
8 10 b
6 4 b
5 4 r
9 10 b
7 3 r
5 6 r
6 8 b
7 4 b
8 9 r
9 9 r
3 6 b
9 7 b

output:

21

result:

ok single line: '21'