Submission #1439746


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

int V, E, src, snk;
vector<int> la, nxt, oppo, capa;
void init() {
    E = 0;
    la.clear(); nxt.clear(); oppo.clear(); capa.clear();
    la = vector<int>(V, -1);
}
void add(int u, int v, int c) {
    nxt.push_back(la[u]);
    la[u] = E++;
    oppo.push_back(v);
    capa.push_back(c);
}
vector<int> dist;
queue<int> q;
bool bfs() {
    dist = vector<int>(V, -1);
    q.push(src);
    dist[src] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();

        for(int i = la[u]; i != -1; i = nxt[i]) {
            int v = oppo[i];
            if(capa[i] && dist[v] == -1) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
    return dist[snk] != -1;
}
vector<int> laa;
int dfs(int u, int f) {
    if(u == snk) return f;

    for(int i = laa[u]; i != -1; i = nxt[i]) {
        laa[u] = i;
        int v = oppo[i];
        if(capa[i] && dist[v] == dist[u] + 1) {
            if(int tmp = dfs(v, min(capa[i], f))) {
                capa[i] -= tmp;
                capa[i ^ 1] += tmp;
                return tmp;
            }
        }
    }
    return 0;
}
int dinic() {
    int tf = 0;
    while(bfs()) {
        laa = la;
        while(int tmp = dfs(src, 1e8)) tf += tmp;
    }
    return tf;
}

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int H, W;
vector<string> G;

int main() {
    scanf("%d %d", &H, &W);

    G.resize(H);

    for(int i = 0; i < H; i++) {
        cin>>G[i];
    }

    V = 2*H*W + 2, src = V - 2, snk = V - 1;
    init();

    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            add(i*W + j, H*W + i*W + j, (G[i][j] == '.'? 1 : 1e8));
            add(H*W + i*W + j, i*W + j, 0);

            if(G[i][j] == 'X') {
                add(src, i*W + j, 1e8);
                add(i*W + j, src, 0);
            }

            bool edge = false;
            for(int k = 0; k < 4; k++) {
                int ni = i + dy[k];
                int nj = j + dx[k];
                if(ni < 0 || H <= ni || nj < 0 || W <= nj) {
                    edge = true;
                    continue;
                }
                add(H*W + i*W + j, ni*W + nj, 1e8);
                add(ni*W + nj, H*W + i*W + j, 0);
            }

            if(edge) {
                add(H*W + i*W + j, snk, 1e8);
                add(snk, H*W + i*W + j, 0);
            }

            if(edge && G[i][j] == 'X') {
                printf("-1\n");
                return 0;
            }
        }
    }

    printf("%d\n", dinic());
}

Submission Info

Submission Time
Task E - Fences
User choikiwon
Language C++14 (GCC 5.4.1)
Score 150
Code Size 2677 Byte
Status AC
Exec Time 40 ms
Memory 1772 KB

Compile Error

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

Judge Result

Set Name All
Score / Max Score 150 / 150
Status
AC × 19
Set Name Test Cases
All 00_sample.txt, 01_sample.txt, 02_sample.txt, 10_rand_00.txt, 10_rand_01.txt, 10_rand_02.txt, 10_rand_03.txt, 10_rand_04.txt, 10_rand_05.txt, 10_rand_06.txt, 10_rand_07.txt, 10_rand_08.txt, 11_hashi_00.txt, 11_hashi_01.txt, 11_hashi_02.txt, 12_rect_00.txt, 12_rect_01.txt, 12_rect_02.txt, 99_all_one.txt
Case Name Status Exec Time Memory
00_sample.txt AC 1 ms 256 KB
01_sample.txt AC 1 ms 256 KB
02_sample.txt AC 1 ms 256 KB
10_rand_00.txt AC 1 ms 256 KB
10_rand_01.txt AC 2 ms 256 KB
10_rand_02.txt AC 10 ms 768 KB
10_rand_03.txt AC 11 ms 768 KB
10_rand_04.txt AC 5 ms 1772 KB
10_rand_05.txt AC 10 ms 1772 KB
10_rand_06.txt AC 5 ms 768 KB
10_rand_07.txt AC 11 ms 1148 KB
10_rand_08.txt AC 21 ms 1656 KB
11_hashi_00.txt AC 1 ms 256 KB
11_hashi_01.txt AC 2 ms 640 KB
11_hashi_02.txt AC 1 ms 256 KB
12_rect_00.txt AC 40 ms 1532 KB
12_rect_01.txt AC 6 ms 640 KB
12_rect_02.txt AC 6 ms 1532 KB
99_all_one.txt AC 2 ms 384 KB