#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define pil pair<int, long long> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 30 * 1000 + 10; int vis[2 * maxN]; ll odpow[maxN]; pil magic[2 * maxN]; vector<pil> g[2 * maxN]; void dfs(int v) { vis[v] = 1; for (auto [u, w] : g[v]) if (!vis[u]) { magic[u].st = magic[v].st * -1; magic[u].nd = w - magic[v].nd; dfs(u); } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vi> costs(n, vi(m)); rep(i, n) rep(j, m) cin >> costs[i][j]; vector<vector<ll> > opt(n, vector<ll>(m)); const ll inf = 1e18; rep(s, n) { fwd(i, s, n) fill(all(opt[i]), inf); opt[s][0] = 0; fwd(i, s, n) { rep(j, m - 1) if (opt[i][j] + costs[i][j] < opt[i][j + 1]) opt[i][j + 1] = opt[i][j] + costs[i][j]; if (i + 1 != n) { fwd(j, 1, m) opt[i + 1][j] = opt[i][j]; } } fwd(t, s, n) { ll w = opt[t][m - 1] + costs[t][m - 1]; g[s].push_back({n + t, w}); g[n + t].push_back({s, w}); } } magic[0] = {1, 0}; dfs(0); rep(v, 2 * n) { magic[v].st *= 2; magic[v].nd *= 2; for (auto &[u, w] : g[v]) w *= 2; } ll xval = 0; bool xval_set = false; rep(v, 2 * n) { for (auto [u, w] : g[v]) if ((magic[v].st + magic[u].st) != 0) { xval = (w - magic[v].nd - magic[u].nd) / 2; if (magic[v].st < 0) xval *= -1; xval_set = true; break; } if (xval_set) break; } rep(v, 2 * n) magic[v].nd += xval * magic[v].st; bool works = true; rep(v, 2 * n) { for (auto [u, w] : g[v]) if ((magic[v].nd + magic[u].nd) != w) { works = false; break; } if (!works) break; } if (works) { cout << 2 << '\n'; if (q == 1) { rep(i, n) cout << magic[i].nd / 2 << ' ' << magic[n + i].nd / 2 << '\n'; } } else { cout << 3 << '\n'; if (q == 1) { fill(odpow, odpow + n, -inf); rep(v, n) for (auto [u, w] : g[v]) odpow[u - n] = max(odpow[u - n], w / 2); rep(i, n) cout << 0 << ' ' << odpow[i] << ' ' << 0 << '\n'; } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define pil pair<int, long long> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 30 * 1000 + 10; int vis[2 * maxN]; ll odpow[maxN]; pil magic[2 * maxN]; vector<pil> g[2 * maxN]; void dfs(int v) { vis[v] = 1; for (auto [u, w] : g[v]) if (!vis[u]) { magic[u].st = magic[v].st * -1; magic[u].nd = w - magic[v].nd; dfs(u); } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vi> costs(n, vi(m)); rep(i, n) rep(j, m) cin >> costs[i][j]; vector<vector<ll> > opt(n, vector<ll>(m)); const ll inf = 1e18; rep(s, n) { fwd(i, s, n) fill(all(opt[i]), inf); opt[s][0] = 0; fwd(i, s, n) { rep(j, m - 1) if (opt[i][j] + costs[i][j] < opt[i][j + 1]) opt[i][j + 1] = opt[i][j] + costs[i][j]; if (i + 1 != n) { fwd(j, 1, m) opt[i + 1][j] = opt[i][j]; } } fwd(t, s, n) { ll w = opt[t][m - 1] + costs[t][m - 1]; g[s].push_back({n + t, w}); g[n + t].push_back({s, w}); } } magic[0] = {1, 0}; dfs(0); rep(v, 2 * n) { magic[v].st *= 2; magic[v].nd *= 2; for (auto &[u, w] : g[v]) w *= 2; } ll xval = 0; bool xval_set = false; rep(v, 2 * n) { for (auto [u, w] : g[v]) if ((magic[v].st + magic[u].st) != 0) { xval = (w - magic[v].nd - magic[u].nd) / 2; if (magic[v].st < 0) xval *= -1; xval_set = true; break; } if (xval_set) break; } rep(v, 2 * n) magic[v].nd += xval * magic[v].st; bool works = true; rep(v, 2 * n) { for (auto [u, w] : g[v]) if ((magic[v].nd + magic[u].nd) != w) { works = false; break; } if (!works) break; } if (works) { cout << 2 << '\n'; if (q == 1) { rep(i, n) cout << magic[i].nd / 2 << ' ' << magic[n + i].nd / 2 << '\n'; } } else { cout << 3 << '\n'; if (q == 1) { fill(odpow, odpow + n, -inf); rep(v, n) for (auto [u, w] : g[v]) odpow[u - n] = max(odpow[u - n], w / 2); rep(i, n) cout << 0 << ' ' << odpow[i] << ' ' << 0 << '\n'; } } return 0; } |