結果

問題 No.421 しろくろチョコレート
ユーザー te-sh
提出日時 2017-10-26 16:56:20
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 4,395 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 118,988 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 22:09:16
合計ジャッジ時間 2,869 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import std.algorithm, std.conv, std.range, std.stdio, std.string;
alias point = Point!int;
alias grid = Grid!(bool, int);
alias graph = Graph!(int, int);
alias edge = graph.Edge;
void main()
{
auto rd = readln.split.to!(int[]), r = rd[0], c = rd[1], n = r*c;
auto g = grid(r, c);
foreach (i; 0..r) {
auto s = readln.chomp;
foreach (j; 0..c)
g[i][j] = s[j] != '.';
}
auto idx(point p) { return p.y*c+p.x; }
auto h = new edge[][](n+2), nw = 0, nb = 0;
foreach (i; 0..r)
foreach (j; 0..c) {
auto p = point(j, i);
if ((i+j) % 2 == 0 && g[i][j]) {
++nw;
h[n] ~= edge(n, idx(p), 1);
foreach (np; g.sibPoints4(p).filter!(np => g[np]))
h[idx(p)] ~= edge(idx(p), idx(np), 1);
} else if ((i+j) % 2 == 1 && g[i][j]) {
++nb;
h[idx(p)] ~= edge(idx(p), n+1, 1);
}
}
auto w1 = graph.fordFulkerson(h, n, n+1);
nw -= w1;
nb -= w1;
auto w2 = min(nw, nb);
nw -= w2;
nb -= w2;
writeln(w1 * 100 + w2 * 10 + nw + nb);
}
struct Point(T)
{
T x, y;
pure auto opBinary(string op: "+")(Point!T rhs) const { return Point!T(x + rhs.x, y + rhs.y); }
pure auto opBinary(string op: "-")(Point!T rhs) const { return Point!T(x - rhs.x, y - rhs.y); }
pure auto opBinary(string op: "*")(Point!T rhs) const { return x * rhs.x + y * rhs.y; }
pure auto opBinary(string op: "*")(T a) const { return Point!T(x * a, y * a); }
pure auto opBinary(string op: "/")(T a) const { return Point!T(x / a, y / a); }
pure auto hypot2() const { return x ^^ 2 + y ^^ 2; }
}
struct Grid(T, U)
{
import std.algorithm, std.conv, std.range, std.traits, std.typecons;
const sibs4 = [Point!U(-1, 0), Point!U(0, -1), Point!U(1, 0), Point!U(0, 1)];
const sibs8 = [Point!U(-1, 0), Point!U(-1, -1), Point!U(0, -1), Point!U(1, -1),
Point!U(1, 0), Point!U(1, 1), Point!U(0, 1), Point!U(-1, 1)];
T[][] m;
const size_t rows, cols;
mixin Proxy!m;
this(size_t r, size_t c) { rows = r; cols = c; m = new T[][](rows, cols); }
this(T[][] s) { rows = s.length; cols = s[0].length; m = s; }
pure auto dup() const { return Grid(m.map!(r => r.dup).array); }
ref pure auto opIndex(Point!U p) { return m[p.y][p.x]; }
ref pure auto opIndex(size_t y) { return m[y]; }
ref pure auto opIndex(size_t y, size_t x) const { return m[y][x]; }
static if (isAssignable!T) {
auto opIndexAssign(T v, Point!U p) { return m[p.y][p.x] = v; }
auto opIndexAssign(T v, size_t y, size_t x) { return m[y][x] = v; }
auto opIndexOpAssign(string op, V)(V v, Point!U p) { return mixin("m[p.y][p.x] " ~ op ~ "= v"); }
auto opIndexOpAssign(string op, V)(V v, size_t y, size_t x) { return mixin("m[y][x] " ~ op ~ "= v"); }
}
pure auto validPoint(Point!U p) { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; }
pure auto points() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; }
pure auto sibPoints4(Point!U p) { return sibs4.map!(s => p + s).filter!(p => validPoint(p)); }
pure auto sibPoints8(Point!U p) { return sibs8.map!(s => p + s).filter!(p => validPoint(p)); }
}
template Graph(Wt, Node, Wt _inf = 10 ^^ 9, Node _sent = Node.max)
{
import std.algorithm, std.container, std.conv;
const inf = _inf, sent = _sent;
struct Edge { Node src, dst; Wt cap; }
struct EdgeR { Node src, dst; Wt cap, flow; Node rev; }
Wt fordFulkerson(Edge[][] g, Node s, Node t)
{
auto n = g.length;
auto adj = withRev(g, n);
auto visited = new bool[](n);
Wt augment(Node u, Wt cur)
{
if (u == t) return cur;
visited[u] = true;
foreach (ref e; adj[u]) {
if (!visited[e.dst] && e.cap > e.flow) {
auto f = augment(e.dst, min(e.cap - e.flow, cur));
if (f > 0) {
e.flow += f;
adj[e.dst][e.rev].flow -= f;
return f;
}
}
}
return 0;
}
Wt flow;
for (;;) {
visited[] = false;
auto f = augment(s, inf);
if (f == 0) break;
flow += f;
}
return flow;
}
EdgeR[][] withRev(Edge[][] g, size_t n)
{
auto r = new EdgeR[][](n);
foreach (gi; g)
foreach (e; gi) {
r[e.src] ~= EdgeR(e.src, e.dst, e.cap, 0, cast(Node)(r[e.dst].length));
r[e.dst] ~= EdgeR(e.dst, e.src, 0, 0, cast(Node)(r[e.src].length) - 1);
}
return r;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0