From 3e1b4e4ced22415489d673c89a197fb606a52d94 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 15 Oct 2022 16:32:05 +0800 Subject: [PATCH] =?UTF-8?q?P7515=20[=E7=9C=81=E9=80=89=E8=81=94=E8=80=83?= =?UTF-8?q?=202021=20A=20=E5=8D=B7]=20=E7=9F=A9=E9=98=B5=E6=B8=B8=E6=88=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/90003104 --- Luogu/P7515/P7515.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Luogu/P7515/P7515.cpp diff --git a/Luogu/P7515/P7515.cpp b/Luogu/P7515/P7515.cpp new file mode 100644 index 00000000..ed16d658 --- /dev/null +++ b/Luogu/P7515/P7515.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int MAX = 1e6; + +void solve() { + int n, m; + + cin >> n >> m; + + std::vector> + a(n, std::vector(m, 0)), + b(n - 1, std::vector(m - 1, 0)); + std::vector>> + g(n + m, std::vector>()); + + for (auto &i : b) { + for (auto &x : i) { + cin >> x; + } + } + + for (int i = n - 2; i >= 0; i--) { + for (int j = m - 2; j >= 0; j--) { + a[i][j] = b[i][j] - a[i + 1][j] - a[i + 1][j + 1] - a[i][j + 1]; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if ((i + j) & 1) { + g[i].emplace_back(j + n, a[i][j]); + g[j + n].emplace_back(i, MAX - a[i][j]); + } else { + g[j + n].emplace_back(i, a[i][j]); + g[i].emplace_back(j + n, MAX - a[i][j]); + } + } + } + + std::vector dist(n + m); + + auto spfa = [&]() -> bool { + std::queue q; + std::vector vis(n + m, true); + std::vector cnt(n + m); + + for (int i = 0; i < n + m; i++) { + q.emplace(i); + } + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + vis[u] = false; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + + if (!vis[v]) { + if (++cnt[v] > n + m) return false; + + vis[v] = true; + q.emplace(v); + } + } + } + } + + return true; + }; + + if (!spfa()) { + cout << "NO" << endl; + + return; + } + + cout << "YES" << endl; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cout << a[i][j] + ((i + j) & 1 ? dist[i] - dist[j + n] : dist[j + n] - dist[i]) << ' '; + } + + cout << endl; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + + cin >> t; + + while (t--) solve(); + + return 0; +}