結果

問題 No.459 C-VS for yukicoder
ユーザー maimai
提出日時 2017-01-07 20:54:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,930 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 187,860 KB
実行使用メモリ 192,240 KB
最終ジャッジ日時 2023-08-22 16:50:39
合計ジャッジ時間 8,370 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,388 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 18 ms
4,380 KB
testcase_16 AC 10 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
権限があれば一括ダウンロードができます

ソースコード

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())

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) < 1000);}

// TODO:マーカーの実装?
class Flow{
public:
    size_t n;
    struct Arrow{
        int from,to;
        int w_max;
        
        Arrow(int from=0,int to=0,int w=1):from(from),to(to),w_max(w){}
    };
    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 w_max){
        vertex_to[from].push_back(arrow.size()); // toto
        vertex_from[to].push_back(arrow.size()); // fromfrom
        arrow.emplace_back(from,to,w_max);
    }
    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 _dinic_path_dfs(Flow& flow, vector<int>& result, const vector<int>& dist, int i_source, int v, int mini){
    // TODO: 経路再利用
    if (i_source == v) return 0;
    for (int e : flow.vertex_from[v]){
        Flow::Arrow& a = flow.arrow[e];
        if (a.w_max > 0 && dist[a.from]==dist[v]-1){
            int w ;
            if (mini < 0)
                w = a.w_max;
            else
                w = min(a.w_max, mini);
            
            if (dist[v] <= dist[i_source]+1)
                ;
            else
                w = min(a.w_max,_dinic_path_dfs(flow, result, dist, i_source, a.from, w));
                
            a.w_max -= w;
            result[a.from] += w;
            
            //printf("%d->%d (%d) : %d,%d \n",a.from,a.to,a.w_max,w,mini);
            
            return w;
        }
    }
    return 0;
}

// flowは書き換えられる.
void dinic(Flow &flow, vector<int>& result, int i_source, int i_sink){
    assert(i_source != i_sink);
    
    result.resize(flow.n);
    //fill(result.begin(), result.end(),0);
    
    int distbegin=0;
    vector<int> dist(flow.n);
    queue<int> q;
    
    while (true){
        //safebreak();
        
        q.emplace(i_source);
        dist[i_source] = distbegin+1;
        while (!q.empty()){
            int v = q.front();
            q.pop();
            for (int ie : flow.vertex_to[v]){
                const Flow::Arrow& e = flow.arrow[ie];
                if (0<e.w_max && dist[e.to] <= distbegin){
                    dist[e.to] = dist[v]+1;
                    q.emplace(e.to);
                }
            }
        }
        //debugv(dist);
        
        if (dist[i_sink] <= distbegin){
            break;
        }else{
            int r;
            while (0 < (r = _dinic_path_dfs(flow,result,dist,i_source,i_sink,-1)) )
                result[i_sink]+=r;
        }
        distbegin+=flow.n;
    }
    
}

// 最大最小流量制限付き
class FlowMinMax{
public:
    Flow flow;
    int ss; // vertex of new source
    
    vector<int> w_before;
    
    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 w_max){
        // assert(w_min < w_max);
        
        flow.connect(from, to  , w_max-w_min);
        w_before.emplace_back(w_max-w_min);
        
        flow.connect(from, ss+1, w_min); // TODO: 多重辺が出来ることがある
        w_before.emplace_back(w_min);
        
        flow.connect(ss  , to  , w_min);
        w_before.emplace_back(w_min);
    }
    
    // bool solve_dinic_vertex(vector<int>& result, int i_source, int i_sink){
    //     result.resize(flow.n-2);
    //     
    //     vector<int> resflow(flow.n,0);
    //     
    //     dinic(flow, resflow, ss      , ss+1);
    //     dinic(flow, resflow, ss      , i_sink);
    //     dinic(flow, resflow, i_source, ss+1);
    //     dinic(flow, resflow, i_source, i_sink);
    //     
    //     for (int e : flow.vertex_to[ss+1]){
    //         const Flow::Arrow& a = flow.arrow[e];
    //         if (0 < a.w_max) return false;
    //     }
    //     
    //     for (int u=0; u<flow.n-2; u++){ // todo:bug
    //         int ec=x_sink[u];
    //         const Flow::Arrow& c = flow.arrow[ec];
    //         if (0 < c.w_max) return false;
    //         
    //         for (int ea : flow.vertex_to[u]){
    //             const Flow::Arrow& a = flow.arrow[ea];
    //             if (a.to >= flow.n-2) continue;
    //             
    //             int floow = w_before[ec] + w_before[ea] - c.w_max - a.w_max; // TODO: 
    //             
    //             result[u] += floow;
    //             if (a.to == i_sink) result[i_sink] += floow;
    //         }
    //     }
    //     return true;
    // }
    bool solve_dinic_edge(map<pair<int,int>,int>& result, int i_source, int i_sink){
        
        vector<int> resflow(flow.n,0);
        
        dinic(flow, resflow, ss      , ss+1);
        dinic(flow, resflow, ss      , i_sink);
        dinic(flow, resflow, i_source, ss+1);
        dinic(flow, resflow, i_source, i_sink);
        
        for (int e : flow.vertex_to[ss]){
            const Flow::Arrow& a = flow.arrow[e];
            if (0 < a.w_max) return false;
        }
        
        for (int u=0; u<flow.n-2; u++){
            
            for (int ea : flow.vertex_to[u]){
                const Flow::Arrow& a = flow.arrow[ea];
                if (a.to >= flow.n-2) continue;
                
                int ec = ea+1;
                const Flow::Arrow& c = flow.arrow[ec];
                if (0 < c.w_max) return false;
                
                int floow = w_before[ec] + w_before[ea] - c.w_max - a.w_max; // TODO: 
                
                result[make_pair(u,a.to)]+=floow;
            }
        }
        return true;
    }
    
};

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();
    
    // X座標にブロックがいくつ積まれているか、を記録する。
    // stringを保持する必要はない。
    for (y = 0; y < height; y++){
        string s;
        cin >> s;
        for (x = 0; x < width; x++){
            field[x] += s[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++){
        flow.connect(i_source, 2+i, 1, 9);
        int left = commands[i];
        for (j = 0; j < 3; j++){
            flow.connect(2+i, 2+n+ left+j, 0, 3);
        }
    }
    for (x = 0; x < width; x++){
        flow.connect(2+n+x, i_sink, field[x], field[x]);
    }
    
    map<pair<int,int>,int> nagare;
    if (!flow.solve_dinic_edge(nagare, i_source, i_sink)){
        // abort();
        cout << "warn" << endl;
    }
    
    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');
        }
    }
    
    return 0;
}
0