提出 #16973807
ソースコード 拡げる
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cstring>
#include <queue>
#include <assert.h>
#include <cmath>
#include <stack>
#include <deque>
#include <set>
#include <unordered_map>
#include <complex>
#include <fstream>
#include <map>
#include <numeric>
#include <functional>
using namespace std;
using ll = long long;
const int mxn = 2510;
const int inf = 1e9;
char matrix[mxn][mxn];
vector<int> adj[mxn];
int cost[mxn][mxn], capacity[mxn][mxn], d[mxn], p[mxn];
const int INF = 1e9;
void shortest_paths(int n, int v0) {
for(int i=0; i<n; i++) d[i] = INF;
d[v0] = 0;
vector<bool> inq(n, false);
queue<int> q;
q.push(v0);
for(int i=0; i<n; i++) p[i] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = false;
for (int v : adj[u]) {
if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) {
d[v] = d[u] + cost[u][v];
p[v] = u;
if (!inq[v]) {
inq[v] = true;
q.push(v);
}
}
}
}
}
int min_cost_flow(int N, int K, int s, int t) {
int flow = 0;
int cost = 0;
while (flow < K) {
shortest_paths(N, s);
if (d[t] == INF)
break;
// find max flow on that path
int f = K - flow;
int cur = t;
while (cur != s) {
f = min(f, capacity[p[cur]][cur]);
cur = p[cur];
}
// apply flow
flow += f;
cost += f * d[t];
cur = t;
while (cur != s) {
capacity[p[cur]][cur] -= f;
capacity[cur][p[cur]] += f;
cur = p[cur];
}
}
if (flow < K)
return -1;
else
return cost;
}
void add_edge(int from, int to, int cap, int co) {
adj[from].push_back(to); adj[to].push_back(from);
capacity[from][to] = cap;
cost[from][to] = co;
cost[to][from] = -co;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; cin>>n>>m;
for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>matrix[i][j];
int s = m*n, t = m*n+1, cnt = 0;
for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(matrix[i][j]=='o') add_edge(s, i*m+j, 1, 0), cnt++;
for(int i=0; i<n; i++) for(int j=0; j<m; j++) if(matrix[i][j]=='o' or matrix[i][j]=='.') add_edge(i*m+j, t, 1, 0);
for(int i=0; i<n; i++) for(int j=0; j<m; j++) {
if(i+1<n and matrix[i][j]!='#' and matrix[i+1][j]!='#') add_edge(i*m+j, i*m+j+m, inf, -1);
if(j+1<m and matrix[i][j]!='#' and matrix[i][j+1]!='#') add_edge(i*m+j, i*m+j+1, inf, -1);
}
cout << -min_cost_flow(n*m+2, cnt, s, t) << '\n';
return 0;
}
//comparator function in sorting
//swapping arguments
//missing cases in dp
//dividing in modulo
//Beware of overflow and modulo
//Be mindful of overflow
//s is a matrix but forget
//length of ranges
//working with bit using ull
提出情報
| 提出日時 | |
|---|---|
| 問題 | C - Moving Pieces |
| ユーザ | cote |
| 言語 | C++ (GCC 9.2.1 with AC Library) |
| 得点 | 600 |
| コード長 | 3055 Byte |
| 結果 | AC |
| 実行時間 | 90 ms |
| メモリ | 36904 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 600 / 600 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | 00-sample-01.txt, 00-sample-02.txt |
| All | 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 00-sample-01.txt | AC | 8 ms | 3576 KiB |
| 00-sample-02.txt | AC | 2 ms | 4120 KiB |
| 01-01.txt | AC | 2 ms | 3632 KiB |
| 01-02.txt | AC | 7 ms | 6796 KiB |
| 01-03.txt | AC | 2 ms | 5092 KiB |
| 01-04.txt | AC | 16 ms | 15792 KiB |
| 01-05.txt | AC | 30 ms | 26656 KiB |
| 01-06.txt | AC | 5 ms | 5268 KiB |
| 01-07.txt | AC | 70 ms | 30800 KiB |
| 01-08.txt | AC | 72 ms | 30564 KiB |
| 01-09.txt | AC | 80 ms | 30932 KiB |
| 01-10.txt | AC | 73 ms | 30600 KiB |
| 01-11.txt | AC | 68 ms | 30648 KiB |
| 01-12.txt | AC | 90 ms | 36904 KiB |
| 01-13.txt | AC | 86 ms | 33932 KiB |
| 01-14.txt | AC | 54 ms | 27848 KiB |
| 01-15.txt | AC | 33 ms | 24284 KiB |
| 01-16.txt | AC | 20 ms | 20444 KiB |
| 01-17.txt | AC | 69 ms | 26232 KiB |
| 01-18.txt | AC | 66 ms | 24672 KiB |
| 01-19.txt | AC | 55 ms | 24044 KiB |
| 01-20.txt | AC | 46 ms | 23016 KiB |
| 01-21.txt | AC | 41 ms | 20752 KiB |
| 01-22.txt | AC | 27 ms | 20520 KiB |
| 01-23.txt | AC | 32 ms | 19692 KiB |
| 01-24.txt | AC | 24 ms | 19420 KiB |
| 01-25.txt | AC | 24 ms | 16848 KiB |
| 01-26.txt | AC | 16 ms | 14248 KiB |
| 01-27.txt | AC | 16 ms | 15380 KiB |
| 01-28.txt | AC | 40 ms | 15540 KiB |
| 01-29.txt | AC | 44 ms | 16964 KiB |
| 01-30.txt | AC | 39 ms | 15368 KiB |
| 01-31.txt | AC | 44 ms | 16020 KiB |
| 01-32.txt | AC | 37 ms | 16312 KiB |
| 01-33.txt | AC | 28 ms | 15096 KiB |
| 01-34.txt | AC | 29 ms | 14912 KiB |
| 01-35.txt | AC | 23 ms | 15652 KiB |
| 01-36.txt | AC | 26 ms | 14484 KiB |
| 01-37.txt | AC | 18 ms | 14384 KiB |
| 01-38.txt | AC | 20 ms | 14068 KiB |
| 01-39.txt | AC | 15 ms | 12912 KiB |
| 01-40.txt | AC | 15 ms | 12840 KiB |
| 01-41.txt | AC | 15 ms | 12424 KiB |
| 01-42.txt | AC | 16 ms | 12588 KiB |