#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
using ll = long long;
template<typename A, typename B>
ostream& operator<<(ostream& out, const pair<A,B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& c) {
out << "{";
for(auto it = c.begin(); it != c.end(); it++) {
if(it != c.begin()) out << ", ";
out << *it;
}
return out << "}";
}
void upd(ll& x, ll y) {
x = min(x, y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<ll>> v(n, vector<ll>(m));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) cin >> v[i][j];
}
vector<vector<ll>> d(n, vector<ll>(n));
vector<vector<vector<ll>>> dst(n, vector<vector<ll>>(n, vector<ll>(m + 1, 1e18)));
for(int st = 0; st < n; st++) {
dst[st][st][0] = 0;
for(int i = st; i < n; i++) {
for(int j = 0; j <= m; j++) {
if(j) {
upd(dst[st][i][j], dst[st][i][j - 1] + v[i][j - 1]);
}
if(st < i && 0 < j && j < m) {
upd(dst[st][i][j], dst[st][i - 1][j]);
}
}
d[st][i] = dst[st][i][m];
}
}
vector<ll> a(n), b(n);
for(int i = 0; i < n; i++) {
b[i] = d[0][i];
a[i] = d[i][i] - b[i];
}
bool f = true;
for(int i = 0; i < n; i++) {
for(int j = i; j < n; j++) {
if(a[i] + b[j] != d[i][j]) {
f = false;
break;
}
}
if(!f) break;
}
if(f) {
cout << 2 << endl;
if(q) {
for(int i = 0; i < n; i++) cout << a[i] << " " << b[i] << '\n';
}
} else {
cout << 3 << endl;
if(q) {
cout << "0 0 0\n0 1 0\n0 0 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; using ll = long long; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } void upd(ll& x, ll y) { x = min(x, y); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<ll>> v(n, vector<ll>(m)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) cin >> v[i][j]; } vector<vector<ll>> d(n, vector<ll>(n)); vector<vector<vector<ll>>> dst(n, vector<vector<ll>>(n, vector<ll>(m + 1, 1e18))); for(int st = 0; st < n; st++) { dst[st][st][0] = 0; for(int i = st; i < n; i++) { for(int j = 0; j <= m; j++) { if(j) { upd(dst[st][i][j], dst[st][i][j - 1] + v[i][j - 1]); } if(st < i && 0 < j && j < m) { upd(dst[st][i][j], dst[st][i - 1][j]); } } d[st][i] = dst[st][i][m]; } } vector<ll> a(n), b(n); for(int i = 0; i < n; i++) { b[i] = d[0][i]; a[i] = d[i][i] - b[i]; } bool f = true; for(int i = 0; i < n; i++) { for(int j = i; j < n; j++) { if(a[i] + b[j] != d[i][j]) { f = false; break; } } if(!f) break; } if(f) { cout << 2 << endl; if(q) { for(int i = 0; i < n; i++) cout << a[i] << " " << b[i] << '\n'; } } else { cout << 3 << endl; if(q) { cout << "0 0 0\n0 1 0\n0 0 0\n"; } } return 0; } |
English