結果

問題 No.459 C-VS for yukicoder
ユーザー mai
提出日時 2017-04-21 20:57:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 11,980 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 188,048 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-07-20 05:13:02
合計ジャッジ時間 7,371 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 45
権限があれば一括ダウンロードができます

ソースコード

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

#include<bits/stdc++.h>
using namespace std;
typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define debugaar(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define BIGINT 0x7FFFFFFF
#define E107 1000000007ll
void printbit(int u){if(u==0)cout<<0;else{int s=0,k=0;for(;0<u;u>>=1,k++)s=(s<<1)|(u&1);for(;0<k--;s>>=1)cout<<(s&1);}}
#define TIME chrono::system_clock::now()
#define MILLISEC(t) (chrono::duration_cast<chrono::milliseconds>(t).count())
namespace{
std::chrono::system_clock::time_point t;
void tic(){t=TIME;}
void toc(){fprintf(stderr,"TIME : %lldms\n",MILLISEC(TIME-t));}
}
template<typename T1,typename T2>
ostream& operator <<(ostream &o,const pair<T1,T2> p){o<<"("<<p.first<<":"<<p.second<<")";return o;}
void safebreak(){static auto t = TIME;assert (MILLISEC(TIME-t) < 5000);}
namespace std {
template<typename T1,typename T2>
class hash<pair<T1, T2>> {
public:
size_t operator()(const pair<T1, T2>& x) const{
return hash<T1>()(x.first)^hash<T2>()(x.second);
}
};
}
class Flow {
public:
size_t n;
struct Arrow {
int from, to;
int left;
int cap;
Arrow(int from = 0, int to = 0, int w = 1) :from(from), to(to), left(w), cap(w) {}
bool operator<(const Arrow& a) const { return (from<a.from) | (to<a.to) | (left<a.left) | (cap<a.cap); }
bool operator==(const Arrow& 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<Arrow> arrow;
Flow(int n, int m = 5010) :n(n), vertex_to(n), vertex_from(n) { arrow.reserve(m); }
void connect(int from, int to, int left) {
vertex_to[from].push_back(arrow.size()); // toto
vertex_from[to].push_back(arrow.size()); // fromfrom
arrow.emplace_back(from, to, left);
}
size_t degree(int v) {
return vertex_to[v].size() + vertex_from[v].size();
}
size_t degree_in(int v) {
return vertex_from[v].size();
}
size_t degree_out(int v) {
return vertex_to[v].size();
}
};
int _fordFulkerson_path_dfs(Flow& flow, vector<int>& result, vector<int>& visit, int u, int i_sink, int mini) {
if (i_sink == u) return mini;
int sumw = 0;
bool term = true;
visit[u] = true;
for (int e : flow.vertex_to[u]) {
Flow::Arrow& a = flow.arrow[e];
if (a.left > 0 && !visit[a.to]) {
int w;
if (mini < 0)
w = a.left;
else
w = min(a.left, mini);
w = _fordFulkerson_path_dfs(flow, result, visit, a.to, i_sink, w);
if (w == -1) continue;
a.left -= w;
result[a.to] += w;
//printf("%d->%d (%d) : w=%d mini=%d \n",a.from,a.to,a.left+w,w,mini);
sumw += w;
mini -= w;
term = false;
}
}
for (int e : flow.vertex_from[u]) {
Flow::Arrow& a = flow.arrow[e];
if (a.cap > a.left && !visit[a.from]) {
int w;
if (mini < 0)
w = a.cap - a.left;
else
w = min(a.cap - a.left, mini);
w = _fordFulkerson_path_dfs(flow, result, visit, a.from, i_sink, w);
if (w == -1) continue;
a.left += w;
result[a.to] -= w;
//printf("%d<-%d (%d) : w=%d mini=%d \n",a.from,a.to,a.left-w,w,mini);
sumw += w;
mini -= w;
term = false;
}
}
visit[u] = false;
return term ? -1 : sumw;
}
// flow
void fordFulkerson(Flow &flow, vector<int>& result, int i_source, int i_sink) {
assert(i_source != i_sink);
result.resize(flow.n);
vector<int> visit(flow.n);
int res = 1;
while (0 <= res) {
fill(ALL(visit), false);
res = _fordFulkerson_path_dfs(flow, result, visit, i_source, i_sink, -1);
result[i_source] += max(0, res);
}
}
//
class FlowMinMax{
public:
Flow flow;
int ss; // vertex of new source
FlowMinMax(int n,int m):flow(n+2,m),ss(n){}
FlowMinMax(int n):flow(n+2),ss(n){}
void connect(int from, int to, int w_min, int left){
// assert(w_min < left);
/*
flow.connect(from, ss+1, w_min);
flow.connect(from, to , left-w_min);
flow.connect(ss , to , w_min);
return;
*/
if (left == w_min){
flow.connect(ss , to , w_min);
flow.connect(from, ss+1, w_min);
}else if (w_min == 0){
flow.connect(from, to , left-w_min);
}else{
flow.connect(from, ss+1, w_min);
flow.connect(from, to , left-w_min);
flow.connect(ss , to , w_min);
}
}
private:
template<typename MAP_PI> // map<pair<int,int>,int> or unordered_map
bool _solve_dinic_edge(MAP_PI& result_edge, int i_source, int i_sink){
vector<int> resflow(flow.n,0);
fordFulkerson(flow, resflow, ss , ss+1);
fordFulkerson(flow, resflow, ss , i_sink);
fordFulkerson(flow, resflow, i_source, ss+1);
fordFulkerson(flow, resflow, i_source, i_sink);
for (int e : flow.vertex_from[ss+1]){
const Flow::Arrow& a = flow.arrow[e];
//printf("%d->%d (%d)\n",a.from,a.to,a.left);cout.flush();
if (0 < a.left) return false;
}
int floow;
for (int u=0; u<flow.n-2; u++){
for (int ea : flow.vertex_to[u]){ // TODO:(2)
const Flow::Arrow& a = flow.arrow[ea]; // u -> v
if (a.to >= flow.n-2){
if (0 < a.left) return false;
continue;
}
const Flow::Arrow& c = flow.arrow[ea+1]; // S -> v
if (a.to != c.to){
floow = a.cap - a.left;
}else{
if (0 < c.left) return false;
floow = c.cap + a.cap - c.left - a.left;
}
if (0 < floow)
result_edge[make_pair(u,a.to)] += floow;
}
}
return true;
}
// connect2
// sumflow = sink,flow
template<typename MAP_PI>
bool _solve_dinic_edge_known(MAP_PI& result_edge, int i_source, int i_sink, int sumflow){
vector<int> resflow(flow.n,0);
flow.connect(ss, i_source, sumflow);
flow.connect(i_sink, ss+1, sumflow);
fordFulkerson(flow, resflow, ss , ss+1);
for (int e : flow.vertex_from[ss+1]){
const Flow::Arrow& a = flow.arrow[e];
//printf("%d->%d (%d)\n",a.from,a.to,a.left);cout.flush();
if (0 < a.left) return false;
}
int floow;
for (int u=0; u<flow.n-2; u++){
for (int ea : flow.vertex_to[u]){ // TODO:(2)
const Flow::Arrow& a = flow.arrow[ea]; // u -> v
if (a.to >= flow.n-2){
if (0 < a.left) return false;
continue;
}
const Flow::Arrow& c = flow.arrow[ea+1]; // S -> v
if (a.to != c.to){
floow = a.cap - a.left;
}else{
if (0 < c.left) return false;
floow = c.cap + a.cap - c.left - a.left;
}
if (0 < floow)
result_edge[make_pair(u,a.to)] += floow;
}
}
return true;
}
public:
bool solve_dinic_edge(map<pair<int,int>,int>& result_edge, int i_source, int i_sink, int sumflow = -1){
return sumflow<0 ? _solve_dinic_edge(result_edge, i_source, i_sink)
: _solve_dinic_edge_known(result_edge, i_source, i_sink, sumflow);
}
bool solve_dinic_edge(unordered_map<pair<int,int>,int>& result_edge, int i_source, int i_sink, int sumflow = -1){
return sumflow<0 ? _solve_dinic_edge(result_edge, i_source, i_sink)
: _solve_dinic_edge_known(result_edge, i_source, i_sink, sumflow);
}
};
/**
// dinic sample
int main(){
int i,j,k;
int x,y,a,b;
Flow graph(6);
graph.connect(0,1,1);
graph.connect(1,4,1);
graph.connect(4,5,1);
graph.connect(0,3,1);
graph.connect(3,4,1);
graph.connect(1,2,1);
graph.connect(2,5,1);
vector<int> result(6,0);
dinic(graph,result,0,5);
debugv(result);
// FlowMinMax graph2(3);
//
// graph2.connect(0,1,1,2);
// graph2.connect(1,2,3,4);
//
// vector<int> result2(3,0);
// cout << (graph2.solve_dinic(result2,0,2) ? "true" : "false") << endl;
//
// debugv(result2);
return 0;
}
/**/
/**/
int width,height;
int m,n;
int field[10010];
int commands[30010];
int main(){
int i,j,k;
int x,y,a,b;
tic();
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] (pack3x3)
// 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.left == 0) continue;
// printf("%d -> %d\n",ar.from,ar.to);
//}
unordered_map<pair<int,int>,int> nagare;
if (!flow.solve_dinic_edge(nagare, i_source, i_sink, nblocks)){
abort();
cout << "warn" << endl;
}
//debugv(nagare);
int hako[3];
for (i = 0; i < n; i++){
for (j = 0; j < 3; j++){
hako[j] = nagare[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('#');
}else{
putchar('.');
}
}
putchar('\n');
}
}
toc();
return 0;
}
/*
2 4 3
..#.
..##
0
1
0
*/
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0