結果
| 問題 |
No.2328 Build Walls
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-28 15:52:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 6,706 bytes |
| コンパイル時間 | 2,560 ms |
| コンパイル使用メモリ | 208,328 KB |
| 最終ジャッジ日時 | 2025-02-13 14:15:14 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 11 TLE * 23 |
ソースコード
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#pragma region
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvvi = vector<vector<vector<int>>>;
using vpi = vector<pair<int,int>>;
using vpll = vector<pair<ll,ll>>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using pi = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T = int> using spq = priority_queue<T, vector<T>, greater<T>>;
template<typename T = int> using gpq = priority_queue<T>;
#define fi first
#define se second
#define fore(p,a) for (auto p : a)
#define overload4(a,b,c,d,name,...) name
#define rep1(n) for (int i = 0; i < int (n); ++i)
#define rep2(i,n) for (int i = 0; i < int (n); ++i)
#define rep3(i,a,b) for (int i = a; i < int (b); ++i)
#define rep4(i,a,b,c) for (int i = a; i < int (b); i += c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for (int i = n; i--;)
#define rrep2(i,n) for (int i = n; i--;)
#define rrep3(i,a,b) for (int i = b; i-- > (a);)
#define rrep4(i,a,b,c) for (int i = (a) + ((b) - (a) - 1) / (c) * (c); i >= (a); i -= c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define all(container) begin(container), end(container)
#define rall(container) rbegin(container), rend(container)
#define mvvi(name, row, column) vvi name(row, vi(column))
#define mvvll(name, row, column) vvll name(row, vll(column))
#define mvvb(name, row, column) vvb name(row, vb(column))
#define Max(...) max({__VA_ARGS__})
#define Min(...) min({__VA_ARGS__})
#define fin exit(0)
#define popc __builtin_popcount
#define inrange(x, l, r) ((l) <= (x) && (x) < (r))
#define sq(x) ((x) * (x))
#ifdef LOCAL
template<typename T> string internal_vectostr(vector<T> v) { string ret; for (int i = 0; i < (int) v.size(); i++) ret += (i ? ", " : "") + to_string(v[i]); return ret; }
#define dbg(x) cerr << "[DEBUG] " << __LINE__ << ": " << #x << " = " << x << endl
#define dbv(x) cerr << "[DEBUG] " << __LINE__ << ": " << #x << " = { " << internal_vectostr(x) << " }" << endl
#define tabout(tab, n, m) cout << "[DEBUG]\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << tab[i][j] << ' '; cout << endl; }
#else
template<typename T> void internal_vectostr(vector<T> v) {}
#define dbg(x) ((void) 0)
#define dbv(x) ((void) 0)
#define tabout(tab, n, m) ((void) 0)
#endif
constexpr int INF = 1001001001;
constexpr ll LINF = 1LL << 60;
constexpr double EPS = 1e-10;
const double PI = acos(-1);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll fact(ll x) { return x == 0 ? 1 : x * fact(x - 1); }
ll comb(ll n, ll r) { if (r < 0 || n < r) return 0; ll ret = 1; for (ll i = 1; i <= r; i++) { ret *= n; n--; ret /= i; } return ret; }
int Yes(bool f=1) { cout << (f ? "Yes" : "No") << '\n'; return 0; }
int No() { Yes(0); return 0; }
int YES(bool f=1) { cout << (f ? "YES" : "NO") << '\n'; return 0; }
int NO() { YES(0); return 0; }
int yes(bool f=1) { cout << (f ? "yes" : "no") << '\n'; return 0; }
int no() { yes(0); return 0; }
template<class T> int printvec(vector<T> v) { for (int i = 0; i < (int) v.size(); i++) cout << (i ? " " : "") << v[i]; cout << '\n'; return 0; }
struct IoInit { IoInit() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(15) << fixed; cerr << setprecision(15) << fixed; } } io_init;
#pragma endregion
template <class T>
struct Edge {
int rev, from, to; // rev:逆向きの辺の番号
T cap, original_cap;
Edge(int r, int f, int t, T c) : rev(r), from(f), to(t), cap(c), original_cap(c) {}
};
template <class T>
struct Graph {
vector<vector<Edge<T>>> G;
Graph(int n = 0) : G(n) {}
vector<Edge<T>>& operator[](int i) { return G[i]; }
const size_t size() const { return G.size(); }
Edge<T>& redge(Edge<T> e) { // 逆向きの辺を返す
return G[e.to][e.rev]; // 自己ループはないと仮定(あれば G[e.to][e.rev + 1] とする必要がある)
}
void add_edge(int from, int to, T cap) { // 有向辺を加える
G[from].push_back(Edge<T>((int)G[to].size(), from, to, cap));
G[to].push_back(Edge<T>((int)G[from].size() - 1, to, from, 0));
}
};
/* FordFulkerson: Ford-Fulkersonのアルゴリズムで最大流を求める構造体
max_flow(G,s,t):sからtへのグラフGの最大流を求める
副作用:G は最大流の残余ネットワークになる
計算量: O(|f*||E|) (f*:最大流) (この最悪ケースになることはほぼ無い)
*/
template <class T>
struct FordFulkerson {
const T INF = 1e9;
vector<int> used;
FordFulkerson(){};
T dfs(Graph<T>& G, int v, int t, T f) { // 増加可能経路を見つけて増加分のフローを返す
if (v == t) return f;
used[v] = true;
for (auto& e : G[v]) {
if (!used[e.to] && e.cap > 0) {
T d = dfs(G, e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
G.redge(e).cap += d;
return d;
}
}
}
return 0;
}
T max_flow(Graph<T>& G, int s, int t) {
T flow = 0;
while (true) {
used.assign(G.size(), 0);
T f = dfs(G, s, t, INF); // f が増加分のフロー
if (f == 0) {
return flow;
} else {
flow += f;
}
}
return 0;
}
};
int main(){
int h,w;
cin>>h>>w;
mvvi(a,h-2,w);
rep(h-2)rep(j,w)cin>>a[i][j];
rep(h-2)rep(j,w)if(a[i][j]==-1)a[i][j]=INF;
Graph<ll>g(2*h*w);
auto pos=[&](int i,int j){return i*w+j;};
auto in=[](int x){return x;};
auto out=[&](int x){return h*w+x;};
rep(h-2)rep(j,w)g.add_edge(in(pos(i+1,j)),out(pos(i+1,j)),a[i][j]);
rep(h-2)rep(j,w){
if(j<w-1){
g.add_edge(out(pos(i+1,j)),in(pos(i+1,j+1)),INF);
g.add_edge(out(pos(i+1,j+1)),in(pos(i+1,j)),INF);
}
if(i<h-3){
g.add_edge(out(pos(i+1,j)),in(pos(i+2,j)),INF);
g.add_edge(out(pos(i+2,j)),in(pos(i+1,j)),INF);
}
}
rep(j,w)g.add_edge(0,in(pos(1,j)),INF);
rep(j,w)g.add_edge(out(pos(h-2,j)),2*h*w-1,INF);
FordFulkerson<ll>f;
ll res=f.max_flow(g,0,2*h*w-1);
if(res>=INF)cout<<-1<<endl;
else cout<<res<<endl;
}