結果
| 問題 |
No.459 C-VS for yukicoder
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-03-05 23:56:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 76 ms / 2,000 ms |
| コード長 | 14,693 bytes |
| コンパイル時間 | 3,388 ms |
| コンパイル使用メモリ | 240,768 KB |
| 最終ジャッジ日時 | 2025-01-06 21:59:14 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 58 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:340:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
340 | scanf("%d", commands + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
ソースコード
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
using ll = long long int;
#define debugos cout
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto edgeidx:(v)){debugos<<edgeidx<<" ";}debugos<<endl;}
#define debuga(m,f) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(f);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
#define debugaa(m,h,f) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(f);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(remove_reference<remove_const<decltype(l)>::type>::type cnt=0;(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,edgeidx) for(auto cnt=(b);(cnt)!=(edgeidx);++(cnt))
#define diterate(cnt,b,edgeidx) for(auto cnt=(b);(cnt)!=(edgeidx);--(cnt))
const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
}
template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
template<typename T> inline T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> inline T& minset(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(code); }
mt19937_64 randdev(8901016);
template<typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); }
template<> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); }
template<> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); }
#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif defined(__GNUC__)
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
class MaiScanner {
public:
template<typename T> void input_integer(T& var) noexcept {
var = 0; T sign = 1;
int cc = getchar_unlocked();
for (; cc < '0' || '9' < cc; cc = getchar_unlocked())
if (cc == '-') sign = -1;
for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
}
inline int c() noexcept { return getchar_unlocked(); }
inline MaiScanner& operator>>(int& var) noexcept { input_integer<int>(var); return *this; }
inline MaiScanner& operator>>(long long& var) noexcept { input_integer<long long>(var); return *this; }
inline MaiScanner& operator>>(string& var) {
int cc = getchar_unlocked();
for (; !isvisiblechar(cc); cc = getchar_unlocked());
for (; isvisiblechar(cc); cc = getchar_unlocked())
var.push_back(cc);
return *this;
}
template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
};
class MaiPrinter {
public:
template<typename T>
void output_integer(T var) noexcept {
if (var == 0) { putchar_unlocked('0'); return; }
if (var < 0)
putchar_unlocked('-'),
var = -var;
char stack[32]; int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10),
var /= 10;
while (stack_p)
putchar_unlocked(stack[--stack_p]);
}
inline MaiPrinter& operator<<(char c) noexcept { putchar_unlocked(c); return *this; }
inline MaiPrinter& operator<<(int var) noexcept { output_integer<int>(var); return *this; }
inline MaiPrinter& operator<<(long long var) noexcept { output_integer<long long>(var); return *this; }
inline MaiPrinter& operator<<(char* str_p) noexcept { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
inline MaiPrinter& operator<<(const string& str) {
const char* p = str.c_str();
const char* l = p + str.size();
while (p < l) putchar_unlocked(*p++);
return *this;
}
template<typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }
};
}
MaiScanner scanner;
MaiPrinter printer;
class DGraphF {
public:
typedef int cap_t;
size_t n_;
struct Arc {
int from, to;
// 残量
cap_t left;
// 容量
cap_t cap;
Arc(int from = 0, int to = 0, cap_t w = 1) :from(from), to(to), left(w), cap(w) {}
inline bool operator<(const Arc& a) const { return (left != a.left) ? left < a.left : (left < a.left) | (cap < a.cap) | (from < a.from) | (to < a.to); }
inline bool operator==(const Arc& a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); }
};
vector<vector<int>> vertex_to;
vector<vector<int>> vertex_from;
vector<Arc> edges;
DGraphF(int n = 1) :n_(n), vertex_to(n), vertex_from(n) { }
void connect(int from, int to, cap_t left) {
vertex_to[(size_t)from].push_back((int)edges.size()); // toto
vertex_from[(size_t)to].push_back((int)edges.size()); // fromfrom
edges.emplace_back(from, to, left);
}
inline size_t size() const { return n_; }
};
void dinic(DGraphF &graph, vector<DGraphF::cap_t>& result, int i_source, int i_sink) {
assert(i_source != i_sink);
result.resize(graph.n_);
vector<int> dist(graph.n_);
vector<int8_t> visited(graph.n_);
function<DGraphF::cap_t(int, int, DGraphF::cap_t)> _dfs = [&](int u, int i_sink, DGraphF::cap_t mini) -> DGraphF::cap_t {
// DAG
// TODO: 経路再利用
if (i_sink == u) return mini;
if (visited[u]) return -1;
visited[u] = true;
DGraphF::cap_t sumw = 0;
bool term = true;
for (int edgeidx : graph.vertex_to[u]) {
auto& edge = graph.edges[edgeidx];
if (edge.left > 0 && dist[u] > dist[edge.to]) {
DGraphF::cap_t f = (mini < 0) ? edge.left : min(edge.left, mini);
f = _dfs(edge.to, i_sink, f);
if (f == -1) continue;
edge.left -= f;
result[edge.to] += f;
sumw += f;
mini -= f;
term = false;
visited[u] = false; // TODO: 末尾では?
if (mini == 0) return sumw;
}
}
for (int edgeidx : graph.vertex_from[u]) {
auto& edge = graph.edges[edgeidx];
if (edge.cap > edge.left && dist[u] > dist[edge.from]) {
DGraphF::cap_t f = (mini < 0) ? (edge.cap - edge.left) : min(edge.cap - edge.left, mini);
f = _dfs(edge.from, i_sink, f);
if (f == -1) continue;
edge.left += f;
result[edge.to] -= f;
sumw += f;
mini -= f;
term = false;
visited[u] = false;
if (mini == 0) return sumw;
}
}
return term ? -1 : sumw;
};
queue<int> que;
for (int distbegin = 0; ; distbegin += (int)graph.n_) {
// sinkからsourceへの距離を計算.
que.emplace(i_sink);
dist[i_sink] = distbegin + 1;
while (!que.empty()) {
int v = que.front(); que.pop();
for (int edgeidx : graph.vertex_from[v]) {
const auto edge = graph.edges[edgeidx];
if (0 < edge.left && dist[edge.from] <= distbegin) {
dist[edge.from] = dist[v] + 1;
que.push(edge.from);
}
}
for (int edgeidx : graph.vertex_to[v]) {
const auto edge = graph.edges[edgeidx];
if (edge.left < edge.cap && dist[edge.to] <= distbegin) {
dist[edge.to] = dist[v] + 1;
que.push(edge.to);
}
}
}
fill(visited.begin(), visited.end(), false);
if (dist[i_source] <= distbegin)
break;
else
result[i_source] += _dfs(i_source, i_sink, -1);
}
}
// ## 最小流量制限付き最大フロー
// + http://snuke.hatenablog.com/entry/2016/07/10/043918
// + http://yukicoder.me/submissions/137248
// + http://yukicoder.me/submissions/143696
//
// #### 解説
// 最小流量制限付き最大フローは,普通の最大フローに置き換えることができる.
//
// 面倒なので,最小流l,最大流hで頂点uから頂点vへ流れる有向辺を(u,v)[l,h]と表記する.
//
// + s→tな最大最小流量制限付きフローG=(V,E)を考える.最大流量制限付きフローG'を作りたい.
// + 新たに頂点S,Tを作る.
// + (u,v)[c,c+d]がGに存在するとき,G'に(u,v)[0,d],(u,T)[0,c],(S,v)[0,c]を与える.
// + G'に多重辺が出来ることがある.
// + S→T,S→t,s→T,s→tの順に最大流を求める.S,Tに隣接する辺に優先して流すため.
// + S,Tに隣接する辺が全てemptyになっていれば,条件を満たすフローが存在
// + 流量は(u,v)+(u,T)
//
// 事前に全体の流量が把握出来るならば, #137248のように,S→s,t→Tの辺を作ってS→Tを流せばよい
class FlowMinMax {
public:
DGraphF graph;
const int v_source; // vertex of new source
FlowMinMax(int n) :graph(n + 2), v_source(n) {}
private:
bool _solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {
vector<int> resflow(graph.size(), 0);
dinic(graph, resflow, v_source, v_source + 1);
dinic(graph, resflow, v_source, i_sink);
dinic(graph, resflow, i_source, v_source + 1);
dinic(graph, resflow, i_source, i_sink);
for (int e : graph.vertex_from[v_source + 1]) {
const DGraphF::Arc& a = graph.edges[e];
if (0 < a.left) return false;
}
int flow;
for (int u = 0; u < graph.size() - 2; u++) {
for (int ei : graph.vertex_to[u]) { // TODO:最適化の余地あり(らしい)
const DGraphF::Arc& a = graph.edges[ei]; // u -> v
if (a.to >= graph.size() - 2) {
if (0 < a.left) return false;
continue;
}
const DGraphF::Arc& c = graph.edges[ei + 1]; // S -> v
if (a.to != c.to) {
flow = a.cap - a.left;
}
else {
if (0 < c.left) return false;
flow = c.cap + a.cap - c.left - a.left;
}
if (0 < flow)
result_edge[make_pair(u, a.to)] += flow;
}
}
return true;
}
public:
void connect(int from, int to, int w_min, int w_max) {
if (w_max == w_min) {
graph.connect(v_source, to, w_min);
graph.connect(from, v_source + 1, w_min);
}
else if (w_min == 0) {
graph.connect(from, to, w_max - w_min);
}
else {
graph.connect(from, v_source + 1, w_min);
graph.connect(from, to, w_max - w_min);
graph.connect(v_source, to, w_min);
}
}
inline bool solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {
return _solve_dinic_edge(result_edge, i_source, i_sink);
}
};
// https://yukicoder.me/submissions/172443
int width, height;
int m, n;
int field[10010];
int commands[30010];
int main() {
int i, j, k;
int x, y, a, b;
cin >> height >> width >> n;
cin.ignore();
int nblocks = 0;
// X座標にブロックがいくつ積まれているか、を記録する。
// stringを保持する必要はない。
for (y = 0; y < height; y++) {
string s;
cin >> s;
for (x = 0; x < width; x++) {
field[x] += s[x] == '#';
}
}
for (x = 0; x < width; x++) { nblocks += field[x]; }
for (i = 0; i < n; i++) {
scanf("%d", commands + i);
}
// A _ B _ C
// | | ----> | | ----> [sink]
// [source] -> | | | |
// | | | |
// |_pack |_field
//
// A : [1,9] (packは[1,9]個のブロックを持つ)
// B : [0,3] (packは3x3の容量を持つ)
// C : [#,#] (x列には#個のブロックが積み上がっている)
FlowMinMax flow(1 + n + width + 1);
const int i_source = 0;
const int i_sink = 1;
for (i = 0; i < n; i++) {
// A edge
flow.connect(i_source, 2 + i, 1, 9);
int left = commands[i];
for (j = 0; j < 3; j++) {
// B edge
flow.connect(2 + i, 2 + n + left + j, 0, 3);
}
}
for (x = 0; x < width; x++) {
// C edge
flow.connect(2 + n + x, i_sink, field[x], field[x]);
}
//for (Flow::Arrow& ar : flow.flow.arrow){
// if (ar.w_max == 0) continue;
// printf("%d -> %d\n",ar.from,ar.to);
//}
map<pair<int, int>, int> fl;
if (!flow.solve_dinic_edge(fl, i_source, i_sink)) {
abort();
cout << "warn" << endl;
}
//debugv(nagare);
int hako[3];
for (i = 0; i < n; ++i) {
for (j = 0; j < 3; ++j) {
hako[j] = fl[make_pair(2 + i, 2 + n + commands[i] + j)];
}
for (y = 3; 0 < y; --y) {
for (x = 0; x < 3; ++x) {
if (y <= hako[x]) {
putchar_unlocked('#');
}
else {
putchar_unlocked('.');
}
}
putchar_unlocked('\n');
}
}
return 0;
}