Submission #13934922


Source Code Expand

Copy
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

// Ford-Fulkerson 法による 最大流 O( F |E| )
// Bellman-Ford 法による 最小費用流 O( F |V| |E| )
// [条件に注意] Dijkstra 法による 最小費用流 O( F |E| log |V| )

template <typename CapTp=int, typename CostTp=int>
struct Edge {
    int to, rev;
    CapTp cap; CostTp cost;
    bool is_rev;
    Edge(int t, bool f, int r, CapTp ca, CostTp co=0)
        : to(t), rev(r), cap(ca), cost(co), is_rev(f) {}
};

template <typename CapTp=int, typename CostTp=int>
struct Flow {
    using Graph = vector< vector< Edge<CapTp, CostTp> > >;
    Graph G; const CapTp IA; const CostTp IO;
    vector< pair<int, int> > r_edges;
    Flow(int N_, CapTp IA_=1<<29, CostTp IO_=1<<29)
        : G(N_), IA(IA_), IO(IO_), r_edges() {}
    // 辺を追加 (from -> to に流量 ca, コスト co)
    void add_edge(int from, int to, CapTp ca, CostTp co=0) {
        G[from].emplace_back(to, false, G[to].size(), ca, co);
        G[to].emplace_back(from, true, G[from].size() - 1, 0, -co);
        r_edges.emplace_back(to, G[to].size() - 1);
    }
    // k 番目の辺にいくつ流れたか
    CapTp get_flowed_cap(size_t k) {
        if(r_edges.size() <= k) return -1;
        int v, i; tie(v, i) = r_edges[k];
        return G[v][i].cap;
    }
    // s -> t 最大流
    CapTp max_flow(int s, int t) {
        vector<bool> used(G.size());
        auto dfs = [&](auto &&func, int v, int t, CapTp f) -> CapTp {
            if(v == t) return f;
            used[v] = true;
            for(auto &e : G[v]) {
                if(used[e.to] or e.cap == 0) continue;
                CapTp d = func(func, e.to, t, min(f, e.cap));
                if(d == 0) continue;
                e.cap -= d; G[e.to][e.rev].cap += d;
                return d;
            }
            return 0;
        };

        CapTp res(0);
        while(true) {
            fill(used.begin(), used.end(), false);
            CapTp delta = dfs(dfs, s, t, IA);
            if(delta == 0) return res;
            res += delta;
        }
    }
    // ベルマンフォードをつかって最小費用流
    CostTp mincost_flow(int s, int t, CapTp f) {
        vector<CostTp> dist(G.size()); CostTp res(0);
        vector<int> prevv(G.size()), preve(G.size());
        while(f > 0) {
            fill(dist.begin(), dist.end(), IO);
            dist[s] = 0;
            while(1) {
                bool upd = false;
                for(int v=0; v<(int)G.size(); v++) {
                    if(dist[v] == IO) continue;
                    for(size_t i=0; i<G[v].size(); i++) {
                        auto &e = G[v][i];
                        if(e.cap == 0 or dist[e.to] <= dist[v] + e.cost) continue;
                        dist[e.to] = dist[v] + e.cost;
                        prevv[e.to] = v, preve[e.to] = i;
                        upd = true;
                    }
                }
                if(!upd) break;
            }

            if(dist[t] == IO) return -1;
            CapTp d = f;
            for(int v=t; v!=s; v=prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
            f -= d; res += d * dist[t];
            for(int v=t; v!=s; v=prevv[v]) {
                auto &e = G[prevv[v]][preve[v]];
                e.cap -= d, G[v][e.rev].cap += d;
            }
        }
        return res;
    }
    // ポテンシャルの導入により、ダイクストラ法で最小費用流を解く
    // [仮定している条件]
    //     1. グラフに負の閉路が存在しない (流量の 0 初期化のため)
    //        もし存在するならベルマンフォードで負の閉路を見つけ
    //        そこに流せるだけ流してスタート
    //     2. グラフに負の辺が存在しない (pot_0 の計算可能性)
    //        もし存在する場合は最初のみベルマンフォードを使う必要あり
    CostTp fast_mincost_flow(int s, int t, CapTp f) {
        CostTp res = 0;
        vector<CostTp> dist(G.size()), pot(G.size());
        vector<int> prevv(G.size()), preve(G.size());
        while(f > 0) {
            using PT = pair<CostTp, int>;
            priority_queue< PT, vector<PT>, greater<PT> > que;
            fill(dist.begin(), dist.end(), IO);

            dist[s] = 0;
            que.push(make_pair(0, s));
            while(!que.empty()) {
                PT cur = que.top(); que.pop();
                int v = cur.second;
                if(dist[v] < cur.first) continue;
                for(size_t i=0; i<G[v].size(); i++) {
                    auto& e = G[v][i];
                    if(e.cap > 0 and dist[e.to] > dist[v] + e.cost + pot[v] - pot[e.to]) {
                        dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        que.push(make_pair(dist[e.to], e.to));
                    }
                }
            }
            if(dist[t] == IO) {
                return -1;
            }
            for(int v=0; v<(int)G.size(); v++) pot[v] += dist[v];

            CapTp d = f;
            for(int v=t; v!=s; v=prevv[v]) {
                d = min(d, G[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d * pot[t];
            for(int v=t; v!=s; v=prevv[v]) {
                auto& e = G[prevv[v]][preve[v]];
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
        }
        return res;
    }    
};

int main() {
    int H, W; scanf("%d%d", &H, &W);
    int block = 0;
    vector<string> board(H);
    for(int i=0; i<H; i++) {
        cin >> board[i];
        block += count(board[i].begin(), board[i].end(), '#');
    }

    // 自明なケース
    if(block == 0) { cout << 0 << endl; return 0; }
    if(block == H*W) { cout << -1 << endl; return 0; }

    int ans = INF;
    // 行の選び方を全部試す
    for(int bit=0; bit<(1<<H); bit++) {
        vector<int> xs, ys;

        // 選んだ行
        for(int i=0; i<H; i++) if(bit >> i & 1) xs.emplace_back(i);

        // 選んでいない行において、ブロックが存在するような列
        for(int j=0; j<W; j++) {
            for(int i=0; i<H; i++) {
                if(bit >> i & 1 or board[i][j] == '.') continue;
                ys.emplace_back(j);
                break;
            }
        }

        Flow<int> fl(H + W + 2);
        int source = H + W, sink = source + 1;
        int N = xs.size(), M = ys.size();
        for(int i=0; i<H; i++) fl.add_edge(source, i, 1);
        for(int i=0; i<W; i++) fl.add_edge(H+i, sink, 1);
        for(auto x : xs) {
            for(auto y : ys) {
                if(board[x][y] == '.') fl.add_edge(x, H+y, 1);
            }
        }

        // 行と列の二部マッチング
        int bm = fl.max_flow(source, sink);

        // bm が 0 であるとき (どの board[x][y] にもブロックがあるとき)
        // -> xs, ys の union? であってブロックがないマスが存在するか探す
        if(bm == 0) {
            bm = -1;
            for(auto x : xs) for(int y=0; y<W; y++) if(board[x][y] == '.') bm = 0;
            for(auto y : ys) for(int x=0; x<H; x++) if(board[x][y] == '.') bm = 0;
        }
        chmin(ans, N + M - bm);
    }
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task D - 大爆発
User tsutaj
Language C++14 (GCC 5.4.1)
Score 100
Code Size 8423 Byte
Status
Exec Time 955 ms
Memory 256 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:184:36: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int H, W; scanf("%d%d", &H, &W);
                                    ^

Judge Result

Set Name Score / Max Score Test Cases
small 50 / 50 small/00_border_small_01.txt, small/00_border_small_02.txt, small/00_min_small_01.txt, small/00_min_small_02.txt, small/01_rand_small_00.txt, small/01_rand_small_01.txt, small/01_rand_small_02.txt, small/01_rand_small_03.txt, small/01_rand_small_04.txt, small/01_rand_small_05.txt, small/01_rand_small_06.txt, small/01_rand_small_07.txt, small/01_rand_small_08.txt, small/01_rand_small_09.txt, small/02_maxrand_small_00.txt, small/02_maxrand_small_01.txt, small/02_maxrand_small_02.txt, small/02_maxrand_small_03.txt, small/02_maxrand_small_04.txt, small/02_maxrand_small_05.txt, small/02_maxrand_small_06.txt, small/02_maxrand_small_07.txt, small/02_maxrand_small_08.txt, small/02_maxrand_small_09.txt, small/03_randhard_small_00.txt, small/03_randhard_small_01.txt, small/03_randhard_small_02.txt, small/03_randhard_small_03.txt, small/03_randhard_small_04.txt, small/03_randhard_small_05.txt, small/03_randhard_small_06.txt, small/03_randhard_small_07.txt, small/03_randhard_small_08.txt, small/03_randhard_small_09.txt, small/03_randhard_small_10.txt, small/03_randhard_small_11.txt, small/03_randhard_small_12.txt, small/03_randhard_small_13.txt, small/03_randhard_small_14.txt, small/03_randhard_small_15.txt, small/03_randhard_small_16.txt, small/03_randhard_small_17.txt, small/03_randhard_small_18.txt, small/03_randhard_small_19.txt, small/04_black_small_00.txt, small/04_black_small_01.txt, small/04_black_small_02.txt, small/04_black_small_03.txt, small/04_black_small_04.txt, small/0_sample1.txt, small/0_sample2.txt, small/0_sample4.txt
large 50 / 50 small/00_border_small_01.txt, small/00_border_small_02.txt, small/00_min_small_01.txt, small/00_min_small_02.txt, small/01_rand_small_00.txt, small/01_rand_small_01.txt, small/01_rand_small_02.txt, small/01_rand_small_03.txt, small/01_rand_small_04.txt, small/01_rand_small_05.txt, small/01_rand_small_06.txt, small/01_rand_small_07.txt, small/01_rand_small_08.txt, small/01_rand_small_09.txt, small/02_maxrand_small_00.txt, small/02_maxrand_small_01.txt, small/02_maxrand_small_02.txt, small/02_maxrand_small_03.txt, small/02_maxrand_small_04.txt, small/02_maxrand_small_05.txt, small/02_maxrand_small_06.txt, small/02_maxrand_small_07.txt, small/02_maxrand_small_08.txt, small/02_maxrand_small_09.txt, small/03_randhard_small_00.txt, small/03_randhard_small_01.txt, small/03_randhard_small_02.txt, small/03_randhard_small_03.txt, small/03_randhard_small_04.txt, small/03_randhard_small_05.txt, small/03_randhard_small_06.txt, small/03_randhard_small_07.txt, small/03_randhard_small_08.txt, small/03_randhard_small_09.txt, small/03_randhard_small_10.txt, small/03_randhard_small_11.txt, small/03_randhard_small_12.txt, small/03_randhard_small_13.txt, small/03_randhard_small_14.txt, small/03_randhard_small_15.txt, small/03_randhard_small_16.txt, small/03_randhard_small_17.txt, small/03_randhard_small_18.txt, small/03_randhard_small_19.txt, small/04_black_small_00.txt, small/04_black_small_01.txt, small/04_black_small_02.txt, small/04_black_small_03.txt, small/04_black_small_04.txt, small/0_sample1.txt, small/0_sample2.txt, small/0_sample4.txt, large/00_border_large_01.txt, large/00_border_large_02.txt, large/00_doubleslash_large_00.txt, large/00_slash_large_00.txt, large/00_tripleslash_large_00.txt, large/01_rand_large_00.txt, large/01_rand_large_01.txt, large/01_rand_large_02.txt, large/01_rand_large_03.txt, large/01_rand_large_04.txt, large/01_rand_large_05.txt, large/01_rand_large_06.txt, large/01_rand_large_07.txt, large/01_rand_large_08.txt, large/01_rand_large_09.txt, large/02_maxrand_large_00.txt, large/02_maxrand_large_01.txt, large/02_maxrand_large_02.txt, large/02_maxrand_large_03.txt, large/02_maxrand_large_04.txt, large/02_maxrand_large_05.txt, large/02_maxrand_large_06.txt, large/02_maxrand_large_07.txt, large/02_maxrand_large_08.txt, large/02_maxrand_large_09.txt, large/03_randhard_large_00.txt, large/03_randhard_large_01.txt, large/03_randhard_large_02.txt, large/03_randhard_large_03.txt, large/03_randhard_large_04.txt, large/03_randhard_large_05.txt, large/03_randhard_large_06.txt, large/03_randhard_large_07.txt, large/03_randhard_large_08.txt, large/03_randhard_large_09.txt, large/03_randhard_large_10.txt, large/03_randhard_large_11.txt, large/03_randhard_large_12.txt, large/03_randhard_large_13.txt, large/03_randhard_large_14.txt, large/03_randhard_large_15.txt, large/03_randhard_large_16.txt, large/03_randhard_large_17.txt, large/03_randhard_large_18.txt, large/03_randhard_large_19.txt, large/04_black_large_00.txt, large/04_black_large_01.txt, large/04_black_large_02.txt, large/04_black_large_03.txt, large/04_black_large_04.txt, large/05_almostblack_large_00.txt, large/05_almostblack_large_01.txt, large/05_almostblack_large_02.txt, large/05_almostblack_large_03.txt, large/05_almostblack_large_04.txt, large/05_almostblack_large_05.txt, large/05_almostblack_large_06.txt, large/05_almostblack_large_07.txt, large/05_almostblack_large_08.txt, large/05_almostblack_large_09.txt, large/05_almostblack_large_10.txt, large/05_almostblack_large_11.txt, large/05_almostblack_large_12.txt, large/05_almostblack_large_13.txt, large/05_almostblack_large_14.txt, large/05_almostblack_large_15.txt, large/05_almostblack_large_16.txt, large/05_almostblack_large_17.txt, large/05_almostblack_large_18.txt, large/05_almostblack_large_19.txt, large/0_sample3.txt
Case Name Status Exec Time Memory
large/00_border_large_01.txt 740 ms 256 KB
large/00_border_large_02.txt 295 ms 256 KB
large/00_doubleslash_large_00.txt 895 ms 256 KB
large/00_slash_large_00.txt 726 ms 256 KB
large/00_tripleslash_large_00.txt 952 ms 256 KB
large/01_rand_large_00.txt 1 ms 256 KB
large/01_rand_large_01.txt 3 ms 256 KB
large/01_rand_large_02.txt 1 ms 256 KB
large/01_rand_large_03.txt 17 ms 256 KB
large/01_rand_large_04.txt 862 ms 256 KB
large/01_rand_large_05.txt 65 ms 256 KB
large/01_rand_large_06.txt 9 ms 256 KB
large/01_rand_large_07.txt 1 ms 256 KB
large/01_rand_large_08.txt 30 ms 256 KB
large/01_rand_large_09.txt 1 ms 256 KB
large/02_maxrand_large_00.txt 1 ms 256 KB
large/02_maxrand_large_01.txt 797 ms 256 KB
large/02_maxrand_large_02.txt 955 ms 256 KB
large/02_maxrand_large_03.txt 927 ms 256 KB
large/02_maxrand_large_04.txt 951 ms 256 KB
large/02_maxrand_large_05.txt 800 ms 256 KB
large/02_maxrand_large_06.txt 752 ms 256 KB
large/02_maxrand_large_07.txt 647 ms 256 KB
large/02_maxrand_large_08.txt 453 ms 256 KB
large/02_maxrand_large_09.txt 1 ms 256 KB
large/03_randhard_large_00.txt 1 ms 256 KB
large/03_randhard_large_01.txt 616 ms 256 KB
large/03_randhard_large_02.txt 832 ms 256 KB
large/03_randhard_large_03.txt 876 ms 256 KB
large/03_randhard_large_04.txt 953 ms 256 KB
large/03_randhard_large_05.txt 762 ms 256 KB
large/03_randhard_large_06.txt 804 ms 256 KB
large/03_randhard_large_07.txt 905 ms 256 KB
large/03_randhard_large_08.txt 924 ms 256 KB
large/03_randhard_large_09.txt 853 ms 256 KB
large/03_randhard_large_10.txt 903 ms 256 KB
large/03_randhard_large_11.txt 893 ms 256 KB
large/03_randhard_large_12.txt 827 ms 256 KB
large/03_randhard_large_13.txt 820 ms 256 KB
large/03_randhard_large_14.txt 806 ms 256 KB
large/03_randhard_large_15.txt 801 ms 256 KB
large/03_randhard_large_16.txt 502 ms 256 KB
large/03_randhard_large_17.txt 837 ms 256 KB
large/03_randhard_large_18.txt 747 ms 256 KB
large/03_randhard_large_19.txt 282 ms 256 KB
large/04_black_large_00.txt 1 ms 256 KB
large/04_black_large_01.txt 513 ms 256 KB
large/04_black_large_02.txt 506 ms 256 KB
large/04_black_large_03.txt 1 ms 256 KB
large/04_black_large_04.txt 474 ms 256 KB
large/05_almostblack_large_00.txt 33 ms 256 KB
large/05_almostblack_large_01.txt 30 ms 256 KB
large/05_almostblack_large_02.txt 295 ms 256 KB
large/05_almostblack_large_03.txt 41 ms 256 KB
large/05_almostblack_large_04.txt 47 ms 256 KB
large/05_almostblack_large_05.txt 187 ms 256 KB
large/05_almostblack_large_06.txt 12 ms 256 KB
large/05_almostblack_large_07.txt 72 ms 256 KB
large/05_almostblack_large_08.txt 1 ms 256 KB
large/05_almostblack_large_09.txt 24 ms 256 KB
large/05_almostblack_large_10.txt 18 ms 256 KB
large/05_almostblack_large_11.txt 103 ms 256 KB
large/05_almostblack_large_12.txt 342 ms 256 KB
large/05_almostblack_large_13.txt 65 ms 256 KB
large/05_almostblack_large_14.txt 11 ms 256 KB
large/05_almostblack_large_15.txt 69 ms 256 KB
large/05_almostblack_large_16.txt 224 ms 256 KB
large/05_almostblack_large_17.txt 13 ms 256 KB
large/05_almostblack_large_18.txt 9 ms 256 KB
large/05_almostblack_large_19.txt 450 ms 256 KB
large/0_sample3.txt 1 ms 256 KB
small/00_border_small_01.txt 1 ms 256 KB
small/00_border_small_02.txt 1 ms 256 KB
small/00_min_small_01.txt 1 ms 256 KB
small/00_min_small_02.txt 1 ms 256 KB
small/01_rand_small_00.txt 1 ms 256 KB
small/01_rand_small_01.txt 1 ms 256 KB
small/01_rand_small_02.txt 1 ms 256 KB
small/01_rand_small_03.txt 1 ms 256 KB
small/01_rand_small_04.txt 1 ms 256 KB
small/01_rand_small_05.txt 1 ms 256 KB
small/01_rand_small_06.txt 1 ms 256 KB
small/01_rand_small_07.txt 1 ms 256 KB
small/01_rand_small_08.txt 1 ms 256 KB
small/01_rand_small_09.txt 1 ms 256 KB
small/02_maxrand_small_00.txt 1 ms 256 KB
small/02_maxrand_small_01.txt 1 ms 256 KB
small/02_maxrand_small_02.txt 1 ms 256 KB
small/02_maxrand_small_03.txt 1 ms 256 KB
small/02_maxrand_small_04.txt 1 ms 256 KB
small/02_maxrand_small_05.txt 1 ms 256 KB
small/02_maxrand_small_06.txt 1 ms 256 KB
small/02_maxrand_small_07.txt 1 ms 256 KB
small/02_maxrand_small_08.txt 1 ms 256 KB
small/02_maxrand_small_09.txt 1 ms 256 KB
small/03_randhard_small_00.txt 1 ms 256 KB
small/03_randhard_small_01.txt 1 ms 256 KB
small/03_randhard_small_02.txt 1 ms 256 KB
small/03_randhard_small_03.txt 1 ms 256 KB
small/03_randhard_small_04.txt 1 ms 256 KB
small/03_randhard_small_05.txt 1 ms 256 KB
small/03_randhard_small_06.txt 1 ms 256 KB
small/03_randhard_small_07.txt 1 ms 256 KB
small/03_randhard_small_08.txt 1 ms 256 KB
small/03_randhard_small_09.txt 1 ms 256 KB
small/03_randhard_small_10.txt 1 ms 256 KB
small/03_randhard_small_11.txt 1 ms 256 KB
small/03_randhard_small_12.txt 1 ms 256 KB
small/03_randhard_small_13.txt 1 ms 256 KB
small/03_randhard_small_14.txt 1 ms 256 KB
small/03_randhard_small_15.txt 1 ms 256 KB
small/03_randhard_small_16.txt 1 ms 256 KB
small/03_randhard_small_17.txt 1 ms 256 KB
small/03_randhard_small_18.txt 1 ms 256 KB
small/03_randhard_small_19.txt 1 ms 256 KB
small/04_black_small_00.txt 1 ms 256 KB
small/04_black_small_01.txt 1 ms 256 KB
small/04_black_small_02.txt 1 ms 256 KB
small/04_black_small_03.txt 1 ms 256 KB
small/04_black_small_04.txt 1 ms 256 KB
small/0_sample1.txt 1 ms 256 KB
small/0_sample2.txt 1 ms 256 KB
small/0_sample4.txt 1 ms 256 KB