QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#399226 | #1197. Draw in Straight Lines | hos_lyric | WA | 4ms | 4352kb | C++14 | 5.5kb | 2024-04-26 07:45:59 | 2024-04-26 07:46:00 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
template <class Flow> struct MaxFlow {
// Watch out when using types other than int and long long.
static constexpr Flow FLOW_EPS = 1e-10L;
static constexpr Flow FLOW_INF = std::numeric_limits<Flow>::max();
int n, m;
vector<int> ptr, nxt, zu;
vector<Flow> capa;
explicit MaxFlow(int n_) : n(n_), m(0), ptr(n_, -1) {}
void ae(int u, int v, Flow w0, Flow w1 = 0) {
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
assert(0 <= w0);
assert(0 <= w1);
nxt.push_back(ptr[u]); zu.push_back(v); capa.push_back(w0); ptr[u] = m++;
nxt.push_back(ptr[v]); zu.push_back(u); capa.push_back(w1); ptr[v] = m++;
}
vector<int> see, lev, que;
Flow augment(int u, int t, Flow limFlow) {
if (u == t) return limFlow;
for (int &i = see[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
const int v = zu[i];
if (lev[u] < lev[v]) {
const Flow f = augment(v, t, min(limFlow, capa[i]));
if (f > FLOW_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
}
}
return 0;
}
bool bfs(int s, int t) {
for (int u = 0; u < n; ++u) { see[u] = ptr[u]; lev[u] = -1; }
auto head = que.begin(), tail = que.begin();
for (lev[*tail++ = s] = 0; head != tail; ) {
const int u = *head++;
for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
const int v = zu[i];
if (!~lev[v]) {
lev[*tail++ = v] = lev[u] + 1;
if (v == t) return true;
}
}
}
return false;
}
Flow run(int s, int t, Flow limFlow = FLOW_INF) {
see.resize(n); lev.resize(n); que.resize(n);
Flow flow = 0;
for (; flow + FLOW_EPS < limFlow && bfs(s, t); ) {
for (Flow f; (f = augment(s, t, limFlow - flow)) > FLOW_EPS; flow += f) {}
}
return flow;
}
};
////////////////////////////////////////////////////////////////////////////////
/*
'#'
tate\yoko no black white
no C INF
black
white INF INF INF
'.'
tate\yoko no black white
no C
black C INF
white
tate: 1 <== (not black) <== white <== 0
yoko: 1 <== (not white) <== black <== 0
*/
constexpr int INF = 1001001001;
int M, N, A, B, C;
char S[110][110];
enum { TATE_NOT_BLACK, TATE_WHITE, YOKO_NOT_WHITE, YOKO_BLACK, Z };
int id(int x, int y, int z) {
return (x * N + y) * Z + z;
}
int main() {
for (; ~scanf("%d%d%d%d%d", &M, &N, &A, &B, &C); ) {
for (int x = 0; x < M; ++x) {
scanf("%s", S[x]);
}
MaxFlow<int> mf(M * N * Z + 2);
const int tru = M * N * Z + 0;
const int fal = M * N * Z + 1;
for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
mf.ae(id(x, y, TATE_WHITE), id(x, y, TATE_NOT_BLACK), INF);
mf.ae(id(x, y, YOKO_BLACK), id(x, y, YOKO_NOT_WHITE), INF);
mf.ae(tru, id(x, y, TATE_NOT_BLACK), A);
mf.ae(id(x, y, TATE_WHITE), fal, A);
mf.ae(tru, id(x, y, YOKO_NOT_WHITE), A);
mf.ae(id(x, y, YOKO_BLACK), fal, A);
if (S[x][y] == '#') {
mf.ae(id(x, y, TATE_NOT_BLACK), id(x, y, YOKO_BLACK), C);
mf.ae(id(x, y, TATE_WHITE), fal, INF);
} else if (S[x][y] == '.') {
mf.ae(id(x, y, YOKO_NOT_WHITE), id(x, y, TATE_NOT_BLACK), C);
mf.ae(id(x, y, YOKO_BLACK), id(x, y, TATE_WHITE), C);
mf.ae(id(x, y, YOKO_BLACK), id(x, y, TATE_NOT_BLACK), INF);
} else {
assert(false);
}
}
for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
/*
(x - 1, y, TATE_NOT_BLACK) && (x, y, TATE_BLACK): +B
(x - 1, y, TATE_NOT_WHITE) && (x, y, TATE_WHITE): +B
(x, y - 1, YOKO_NOT_BLACK) && (x, y, YOKO_BLACK): +B
(x, y - 1, YOKO_NOT_WHITE) && (x, y, YOKO_WHITE): +B
*/
mf.ae((x == 0) ? tru : id(x - 1, y, TATE_NOT_BLACK), id(x, y, TATE_NOT_BLACK), B);
mf.ae(id(x, y, TATE_WHITE), (x == 0) ? fal : id(x - 1, y, TATE_WHITE), B);
mf.ae(id(x, y, YOKO_BLACK), (y == 0) ? fal : id(x, y - 1, YOKO_BLACK), B);
mf.ae((y == 0) ? tru : id(x, y - 1, YOKO_NOT_WHITE), id(x, y, YOKO_NOT_WHITE), B);
}
const int ans = mf.run(tru, fal);
printf("%d\n", ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3776kb
input:
3 3 1 2 3 .#. ### .#.
output:
10
result:
ok answer is '10'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
2 7 0 1 1 ###.### ###.###
output:
3
result:
ok answer is '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
5 5 1 4 4 ..#.. ..#.. ##.## ..#.. ..#..
output:
24
result:
ok answer is '24'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3924kb
input:
7 24 1 10 10 ###...###..#####....###. .#...#...#.#....#..#...# .#..#......#....#.#..... .#..#......#####..#..... .#..#......#......#..... .#...#...#.#.......#...# ###...###..#........###.
output:
256
result:
ok answer is '256'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3904kb
input:
5 5 0 3 2 ..#.. ..#.. ##.## ..#.. ..#..
output:
11
result:
ok answer is '11'
Test #6:
score: 0
Accepted
time: 1ms
memory: 4092kb
input:
40 40 40 40 40 ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## #######################################...
output:
64000
result:
ok answer is '64000'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
1 1 0 0 0 .
output:
0
result:
ok answer is '0'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3868kb
input:
9 18 18 39 28 ############.####. ...........#.####. ############.##### ##............#### ................#. #######..######.## .................. ##................ #######.#######.#.
output:
1857
result:
ok answer is '1857'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3976kb
input:
18 22 22 36 36 .##################... .###.................. ..######.#...##.#....# ########.##.########## ............#.###....# ............#.###....# .....................# .#######..#.######...# ########..#.#.###....# ...#......#.####.....# ...#.................# ...#........#.#......# ...#........
output:
4180
result:
ok answer is '4180'
Test #10:
score: 0
Accepted
time: 1ms
memory: 4168kb
input:
5 22 24 33 32 ###################### ...####............... ...................... #####################. ......................
output:
1226
result:
ok answer is '1226'
Test #11:
score: 0
Accepted
time: 0ms
memory: 4240kb
input:
24 23 28 16 33 #................#..... ....................... #...................... ....................... #.....#..........#..... .....................#. .......#............... #...................... ..#.........#.......... .................###... ......................# .............#..........
output:
1151
result:
ok answer is '1151'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3980kb
input:
32 32 13 33 21 ....####...##...#............... .............................#.. .....#.##.#.###.........###..#.# ....................#........#.. ....................#........... ...###.#..#.###.##..#........... ..#.#..#..#.#.......#........##. ..............................#. ........................
output:
4165
result:
ok answer is '4165'
Test #13:
score: 0
Accepted
time: 0ms
memory: 4168kb
input:
4 39 0 36 15 ......####...................#########. ....################################... .###############.###############....... .......................................
output:
159
result:
ok answer is '159'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3992kb
input:
19 14 0 27 11 .............# .#.#####.##### .........##### .#....#......# .#....#......# .#............ .#....#....#.# .#....#....#.# .............. ##.#####.##### ......#...##.# ......#...##.# ......#...##.# ......#...##.# ......#...##.# ......#...##.# ......#...##.# ...........#.# ##############
output:
336
result:
ok answer is '336'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3976kb
input:
25 11 2 21 22 .###.##.#.. ..##....#.# #.#####...# #.#####...# ..###.#...# #.....#...# #.###.#...# #.#####...# ........... #.#####...# #.#.###...# #.#.###...# #.#.###...# #.#.###...# #.#.###...# #.#.##....# #.#.##....# ###.###...# ###.###...# #.........# #.#.###..## ........... .##..##.##. #...###.##...
output:
886
result:
ok answer is '886'
Test #16:
score: 0
Accepted
time: 0ms
memory: 4100kb
input:
40 40 7 23 21 ........................................ ......................#.........#....... ........................................ .....#.....#.......#..#.#..#....##...... ..................#..................... .#.#.#...###.............##.....#....... ...#.................................#.....
output:
2709
result:
ok answer is '2709'
Test #17:
score: -100
Wrong Answer
time: 4ms
memory: 4352kb
input:
40 40 5 30 35 ......#................................. ...##.##.###.###.###.####.##.####.#...#. ......#.#....#..#.#....#.#....#...#...#. ####.#..###.###.#..#.####.#.#.#..#....#. .......#..#....####..#....#...#...#..... #.....###....#.#.#..#..#.##...#..##...#. #....##...............................#....
output:
11410
result:
wrong answer expected '11420', found '11410'