結果
| 問題 | No.1479 Matrix Eraser |
| コンテスト | |
| ユーザー |
SSRS
|
| 提出日時 | 2021-04-16 20:20:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,622 bytes |
| コンパイル時間 | 2,670 ms |
| コンパイル使用メモリ | 198,632 KB |
| 実行使用メモリ | 26,240 KB |
| 最終ジャッジ日時 | 2024-07-02 22:22:05 |
| 合計ジャッジ時間 | 34,244 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 TLE * 1 -- * 2 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
template <typename Cap>
struct ford_fulkerson{
struct edge{
int to, rev;
Cap cap;
edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){
}
};
int N;
vector<vector<edge>> G;
ford_fulkerson(){
}
ford_fulkerson(int N): N(N), G(N){
}
void add_edge(int from, int to, Cap cap){
int id1 = G[from].size();
int id2 = G[to].size();
G[from].push_back(edge(to, id2, cap));
G[to].push_back(edge(from, id1, 0));
}
Cap max_flow(int s, int t){
Cap flow = 0;
while (1){
vector<Cap> m(N, INF);
vector<int> pv(N, -1);
vector<int> pe(N, -1);
vector<bool> used(N, false);
queue<int> Q;
Q.push(s);
used[s] = true;
while (!Q.empty()){
int v = Q.front();
Q.pop();
int cnt = G[v].size();
for (int i = 0; i < cnt; i++){
int w = G[v][i].to;
if (!used[w] && G[v][i].cap > 0){
used[w] = true;
m[w] = min(m[v], G[v][i].cap);
pv[w] = v;
pe[w] = i;
Q.push(w);
}
}
}
if (!used[t]){
break;
}
Cap f = m[t];
for (int i = t; i != s; i = pv[i]){
G[pv[i]][pe[i]].cap -= f;
G[i][G[pv[i]][pe[i]].rev].cap += f;
}
flow += f;
}
return flow;
}
};
int main(){
int H, W;
cin >> H >> W;
vector<vector<int>> A(H, vector<int>(W));
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
cin >> A[i][j];
}
}
map<int, vector<pair<int, int>>> mp;
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
if (A[i][j] > 0){
mp[A[i][j]].push_back(make_pair(i, j));
}
}
}
int ans = 0;
for (auto P : mp){
int cnt = P.second.size();
vector<int> X, Y;
for (int i = 0; i < cnt; i++){
X.push_back(P.second[i].first);
Y.push_back(P.second[i].second);
}
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
sort(Y.begin(), Y.end());
Y.erase(unique(Y.begin(), Y.end()), Y.end());
int Xcnt = X.size(), Ycnt = Y.size();
vector<int> x(cnt), y(cnt);
for (int i = 0; i < cnt; i++){
x[i] = lower_bound(X.begin(), X.end(), P.second[i].first) - X.begin();
y[i] = lower_bound(Y.begin(), Y.end(), P.second[i].second) - Y.begin();
}
ford_fulkerson<int> G(Xcnt + Ycnt + 2);
for (int i = 0; i < Xcnt; i++){
G.add_edge(Xcnt + Ycnt, i, 1);
}
for (int i = 0; i < Ycnt; i++){
G.add_edge(Xcnt + i, Xcnt + Ycnt + 1, 1);
}
for (int i = 0; i < cnt; i++){
G.add_edge(x[i], Xcnt + y[i], 1);
}
ans += G.max_flow(Xcnt + Ycnt, Xcnt + Ycnt + 1);
}
cout << ans << endl;
}
SSRS