fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m;
  4. int cost[1205][1205];
  5.  
  6. // Use:
  7. // Constructor: Dinic dinic(n)
  8. //
  9. // add edges: dinic.addEdge(u, v, c)
  10. //
  11. // trace: for (auto e: dinic.E) {
  12. // if (e.flow && e.cap) {
  13. // cout << e.u << " " << e.v << " " << e.flow << endl;
  14. // }
  15. // }
  16. //
  17. // minCut: i from (0 to dinic.N - 1): dinic.d[i] != dinic.N + 1
  18. struct Edge {
  19. int u, v;
  20. long long cap, flow;
  21. Edge() {}
  22. Edge(int u, int v, long long cap): u(u), v(v), cap(cap), flow(0) {}
  23. };
  24.  
  25. struct Dinic {
  26. int N;
  27. vector<Edge> E;
  28. vector<vector<int>> g;
  29. vector<int> d, pt;
  30. Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}
  31. void addEdge(int u, int v, long long cap) {
  32. if (u != v) {
  33. E.emplace_back(Edge(u, v, cap));
  34. g[u].emplace_back(E.size() - 1);
  35. E.emplace_back(Edge(v, u, 0));
  36. g[v].emplace_back(E.size() - 1);
  37. }
  38. }
  39. bool bfs(int S, int T) {
  40. queue<int> q({S});
  41. fill(d.begin(), d.end(), N + 1);
  42. d[S] = 0;
  43. while(!q.empty()) {
  44. int u = q.front(); q.pop();
  45. if (u == T) break;
  46. for (int k: g[u]) {
  47. Edge &e = E[k];
  48. if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
  49. d[e.v] = d[e.u] + 1;
  50. q.emplace(e.v);
  51. }
  52. }
  53. }
  54. return d[T] != N + 1;
  55. }
  56. long long dfs(int u, int T, long long flow = -1) {
  57. if (u == T || flow == 0) return flow;
  58. for (int &i = pt[u]; i < g[u].size(); ++i) {
  59. Edge &e = E[g[u][i]];
  60. Edge &oe = E[g[u][i]^1];
  61. if (d[e.v] == d[e.u] + 1) {
  62. long long amt = e.cap - e.flow;
  63. if (flow != -1 && amt > flow) amt = flow;
  64. if (long long pushed = dfs(e.v, T, amt)) {
  65. e.flow += pushed;
  66. oe.flow -= pushed;
  67. return pushed;
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. long long maxFlow(int S, int T) {
  74. long long total = 0;
  75. while (bfs(S, T)) {
  76. fill(pt.begin(), pt.end(), 0);
  77. while (long long flow = dfs(S, T))
  78. total += flow;
  79. }
  80. return total;
  81. }
  82. };
  83.  
  84.  
  85. void sub3() {
  86. vector<int> match(n + 1);
  87.  
  88. auto solve = [&](int x) {
  89. int source = 0;
  90. int sink = n + m + 1;
  91. Dinic dinic(sink + 1);
  92.  
  93. for (int i = 1; i <= n; i++) {
  94. dinic.addEdge(source, i, 1);
  95. }
  96.  
  97. for (int i = 1; i <= m; i++) {
  98. dinic.addEdge(i + n, sink, 1);
  99. }
  100.  
  101. for (int i = 1; i <= n; i++) {
  102. for (int j = 1; j <= m; j++) {
  103. if (cost[i][j] >= x) {
  104. dinic.addEdge(i, j + n, 1);
  105. }
  106. }
  107. }
  108.  
  109.  
  110. if (dinic.maxFlow(source, sink) == n) {
  111. for (auto e: dinic.E) {
  112. if (e.flow && e.cap) {
  113. if (e.u > 0 && e.u <= n) {
  114. match[e.u] = e.v - n;
  115. }
  116. }
  117. }
  118. return 1;
  119. }
  120. return 0;
  121. };
  122.  
  123. int lower = 1, upper = 2000;
  124. while (lower < upper) {
  125. int mid = (lower + upper + 1) / 2;
  126. if (solve(mid)) {
  127. lower = mid;
  128. }
  129. else {
  130. upper = mid - 1;
  131. }
  132. }
  133. solve(lower);
  134. for (int i = 1; i <= n; i++) {
  135. cout << 1 << " " << match[i] << "\n";
  136. }
  137. }
  138.  
  139. struct Sub1 {
  140. int dp[15][1 << 12];
  141. int trace[15][1 << 12];
  142. Sub1() {
  143. memset(dp, 0, sizeof(dp));
  144. memset(trace, -1, sizeof(trace));
  145. }
  146.  
  147. void solve() {
  148. dp[0][0] = 1e9;
  149. for (int i = 1; i <= n; i++) {
  150. for (int bit = 0; bit < (1 << m); bit++) {
  151. for (int curBit = 1; curBit < (1 << m); curBit++) {
  152. if (bit & (curBit)) continue;
  153.  
  154.  
  155. int newBit = bit | curBit;
  156.  
  157. int curVal = 0;
  158. for (int j = 1; j <= m; j++) {
  159. if (curBit & (1 << (j - 1))) {
  160. curVal += cost[i][j];
  161. }
  162. }
  163.  
  164. int newVal = min(dp[i - 1][bit], curVal);
  165.  
  166. if (newVal > dp[i][newBit]) {
  167. dp[i][newBit] = newVal;
  168. trace[i][newBit] = curBit;
  169. }
  170. }
  171. }
  172. }
  173.  
  174. int curBit = (1 << m) - 1;
  175. vector<vector<int>> res;
  176. for (int i = n; i >= 1; i--) {
  177. int bit = trace[i][curBit];
  178. curBit -= bit;
  179.  
  180. vector<int> cur;
  181. for (int j = 0; j < m; j++) {
  182. if (bit & (1 << j)) {
  183. cur.push_back(j + 1);
  184. }
  185. }
  186. res.push_back(cur);
  187. }
  188. reverse(res.begin(), res.end());
  189. for (auto &v: res) {
  190. cout << v.size() << " ";
  191. for (auto i: v) cout << i << " ";
  192. cout << "\n";
  193. }
  194. }
  195. };
  196.  
  197. struct Data {
  198. int first, second, index;
  199. };
  200.  
  201. void sub2() {
  202. vector<Data> v;
  203.  
  204. int sumAllB = 0;
  205. for (int i = 1; i <= m; i++) {
  206. v.push_back({cost[1][i], cost[2][i], i});
  207. sumAllB += cost[2][i];
  208. }
  209. sort(v.begin(), v.end(), [](Data a, Data b) {
  210. return a.first - a.second > b.first - b.second;
  211. });
  212.  
  213. vector<int> resA, resB;
  214. int finalRes = 0;
  215.  
  216. for (int z = 0; z <= 50000; z++) {
  217.  
  218. if (z) {
  219. for (int i = 0; i + 1 < v.size(); i++) {
  220. if (rand() % (z / 100 + 2) == 0) swap(v[i], v[i + 1]);
  221. }
  222. }
  223. int sumB = sumAllB;
  224. int res = 0, sumA = 0;
  225. int index = -1;
  226. for (int i = 0; i < m; i++) {
  227. sumA += v[i].first;
  228. sumB -= v[i].second;
  229. if (res < min(sumA, sumB)) {
  230. res = min(sumA, sumB);
  231. index = i;
  232. }
  233. }
  234. vector<int> tmpA, tmpB;
  235. for (int i = 0; i < m; i++) {
  236. if (i <= index) {
  237. tmpA.push_back(v[i].index);
  238. }
  239. else {
  240. tmpB.push_back(v[i].index);
  241. }
  242. }
  243.  
  244. if (res > finalRes) {
  245. resA = tmpA;
  246. resB = tmpB;
  247. finalRes = res;
  248. }
  249. }
  250.  
  251. sort(resA.begin(), resA.end());
  252. sort(resB.begin(), resB.end());
  253.  
  254. cout << resA.size() << " ";
  255. for (auto i: resA) cout << i << " ";
  256. cout << "\n";
  257.  
  258. cout << resB.size() << " ";
  259. for (auto i: resB) cout << i << " ";
  260. cout << "\n";
  261. }
  262.  
  263. int main() {
  264. srand(time(0));
  265. // freopen("input.txt", "r", stdin);
  266. // freopen("bonus.inp", "r", stdin);
  267. // freopen("bonus.out", "w", stdout);
  268. ios::sync_with_stdio(0);
  269. cin.tie(NULL);
  270.  
  271. cin >> n >> m;
  272. for (int i = 1; i <= n; i++) {
  273. for (int j = 1; j <= m; j++) {
  274. cin >> cost[i][j];
  275. }
  276. }
  277.  
  278. if (m <= 12 && n <= 12) {
  279. Sub1().solve();
  280. }
  281. else if (m == n) {
  282. sub3();
  283. }
  284. else {
  285. sub2();
  286. }
  287.  
  288.  
  289.  
  290. return 0;
  291. }
  292.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:1: error: class, interface, or enum expected
#include <bits/stdc++.h>
         ^
Main.java:3: error: class, interface, or enum expected
int n, m;
^
Main.java:4: error: class, interface, or enum expected
int cost[1205][1205];
^
Main.java:18: error: class, interface, or enum expected
struct Edge {
^
Main.java:20: error: class, interface, or enum expected
    long long cap, flow;
    ^
Main.java:21: error: class, interface, or enum expected
    Edge() {}
    ^
Main.java:25: error: class, interface, or enum expected
struct Dinic {
^
Main.java:27: error: class, interface, or enum expected
    vector<Edge> E;
    ^
Main.java:28: error: class, interface, or enum expected
    vector<vector<int>> g;
    ^
Main.java:29: error: class, interface, or enum expected
    vector<int> d, pt;
    ^
Main.java:30: error: class, interface, or enum expected
    Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}
    ^
Main.java:34: error: class, interface, or enum expected
            g[u].emplace_back(E.size() - 1);
            ^
Main.java:35: error: class, interface, or enum expected
            E.emplace_back(Edge(v, u, 0));
            ^
Main.java:36: error: class, interface, or enum expected
            g[v].emplace_back(E.size() - 1);
            ^
Main.java:37: error: class, interface, or enum expected
        }
        ^
Main.java:41: error: class, interface, or enum expected
        fill(d.begin(), d.end(), N + 1);
        ^
Main.java:42: error: class, interface, or enum expected
        d[S] = 0;
        ^
Main.java:43: error: class, interface, or enum expected
        while(!q.empty()) {
        ^
Main.java:44: error: class, interface, or enum expected
            int u = q.front(); q.pop();
                               ^
Main.java:45: error: class, interface, or enum expected
            if (u == T) break;
            ^
Main.java:46: error: class, interface, or enum expected
            for (int k: g[u]) {
            ^
Main.java:48: error: class, interface, or enum expected
                if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
                ^
Main.java:50: error: class, interface, or enum expected
                    q.emplace(e.v);
                    ^
Main.java:51: error: class, interface, or enum expected
                }
                ^
Main.java:55: error: class, interface, or enum expected
    }
    ^
Main.java:58: error: class, interface, or enum expected
        for (int &i = pt[u]; i < g[u].size(); ++i) {
        ^
Main.java:58: error: class, interface, or enum expected
        for (int &i = pt[u]; i < g[u].size(); ++i) {
                             ^
Main.java:58: error: class, interface, or enum expected
        for (int &i = pt[u]; i < g[u].size(); ++i) {
                                              ^
Main.java:60: error: class, interface, or enum expected
            Edge &oe = E[g[u][i]^1];
            ^
Main.java:61: error: class, interface, or enum expected
            if (d[e.v] == d[e.u] + 1) {
            ^
Main.java:63: error: class, interface, or enum expected
                if (flow != -1 && amt > flow) amt = flow;
                ^
Main.java:64: error: class, interface, or enum expected
                if (long long pushed = dfs(e.v, T, amt)) {
                ^
Main.java:66: error: class, interface, or enum expected
                    oe.flow -= pushed;
                    ^
Main.java:67: error: class, interface, or enum expected
                    return pushed;
                    ^
Main.java:68: error: class, interface, or enum expected
                }
                ^
Main.java:72: error: class, interface, or enum expected
    }
    ^
Main.java:75: error: class, interface, or enum expected
        while (bfs(S, T)) {
        ^
Main.java:77: error: class, interface, or enum expected
            while (long long flow = dfs(S, T))
            ^
Main.java:79: error: class, interface, or enum expected
        }
        ^
Main.java:81: error: class, interface, or enum expected
    }
    ^
Main.java:85: error: class, interface, or enum expected
void sub3() {
^
Main.java:88: error: class, interface, or enum expected
    auto solve = [&](int x) {
    ^
Main.java:90: error: class, interface, or enum expected
        int sink = n + m + 1;
        ^
Main.java:91: error: class, interface, or enum expected
        Dinic dinic(sink + 1);
        ^
Main.java:93: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
        ^
Main.java:93: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                        ^
Main.java:93: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                                ^
Main.java:95: error: class, interface, or enum expected
        }
        ^
Main.java:97: error: class, interface, or enum expected
        for (int i = 1; i <= m; i++) {
                        ^
Main.java:97: error: class, interface, or enum expected
        for (int i = 1; i <= m; i++) {
                                ^
Main.java:99: error: class, interface, or enum expected
        }
        ^
Main.java:101: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                        ^
Main.java:101: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                                ^
Main.java:102: error: class, interface, or enum expected
            for (int j = 1; j <= m; j++) {
                            ^
Main.java:102: error: class, interface, or enum expected
            for (int j = 1; j <= m; j++) {
                                    ^
Main.java:105: error: class, interface, or enum expected
                } 
                ^
Main.java:115: error: class, interface, or enum expected
                    }
                    ^
Main.java:119: error: class, interface, or enum expected
        }
        ^
Main.java:121: error: class, interface, or enum expected
    };
    ^
Main.java:123: error: class, interface, or enum expected
    int lower = 1, upper = 2000;
    ^
Main.java:124: error: class, interface, or enum expected
    while (lower < upper) {
    ^
Main.java:126: error: class, interface, or enum expected
        if (solve(mid)) {
        ^
Main.java:128: error: class, interface, or enum expected
        }
        ^
Main.java:131: error: class, interface, or enum expected
        }
        ^
Main.java:134: error: class, interface, or enum expected
    for (int i = 1; i <= n; i++) {
    ^
Main.java:134: error: class, interface, or enum expected
    for (int i = 1; i <= n; i++) {
                    ^
Main.java:134: error: class, interface, or enum expected
    for (int i = 1; i <= n; i++) {
                            ^
Main.java:136: error: class, interface, or enum expected
    }
    ^
Main.java:141: error: class, interface, or enum expected
    int trace[15][1 << 12];
    ^
Main.java:142: error: class, interface, or enum expected
    Sub1() {
    ^
Main.java:144: error: class, interface, or enum expected
        memset(trace, -1, sizeof(trace));
        ^
Main.java:145: error: class, interface, or enum expected
    }
    ^
Main.java:149: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
        ^
Main.java:149: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                        ^
Main.java:149: error: class, interface, or enum expected
        for (int i = 1; i <= n; i++) {
                                ^
Main.java:150: error: class, interface, or enum expected
            for (int bit = 0; bit < (1 << m); bit++) {
                              ^
Main.java:150: error: class, interface, or enum expected
            for (int bit = 0; bit < (1 << m); bit++) {
                                              ^
Main.java:151: error: class, interface, or enum expected
                for (int curBit = 1; curBit < (1 << m); curBit++) {
                                     ^
Main.java:151: error: class, interface, or enum expected
                for (int curBit = 1; curBit < (1 << m); curBit++) {
                                                        ^
Main.java:155: error: class, interface, or enum expected
                    int newBit = bit | curBit;
                    ^
Main.java:157: error: class, interface, or enum expected
                    int curVal = 0;
                    ^
Main.java:158: error: class, interface, or enum expected
                    for (int j = 1; j <= m; j++) {
                    ^
Main.java:158: error: class, interface, or enum expected
                    for (int j = 1; j <= m; j++) {
                                    ^
Main.java:158: error: class, interface, or enum expected
                    for (int j = 1; j <= m; j++) {
                                            ^
Main.java:161: error: class, interface, or enum expected
                        }
                        ^
Main.java:166: error: class, interface, or enum expected
                    if (newVal > dp[i][newBit]) {
                    ^
Main.java:168: error: class, interface, or enum expected
                        trace[i][newBit] = curBit;
                        ^
Main.java:169: error: class, interface, or enum expected
                    }
                    ^
Main.java:175: error: class, interface, or enum expected
        vector<vector<int>> res;
        ^
Main.java:176: error: class, interface, or enum expected
        for (int i = n; i >= 1; i--) {
        ^
Main.java:176: error: class, interface, or enum expected
        for (int i = n; i >= 1; i--) {
                        ^
Main.java:176: error: class, interface, or enum expected
        for (int i = n; i >= 1; i--) {
                                ^
Main.java:178: error: class, interface, or enum expected
            curBit -= bit;
            ^
Main.java:180: error: class, interface, or enum expected
            vector<int> cur;
            ^
Main.java:181: error: class, interface, or enum expected
            for (int j = 0; j < m; j++) {
            ^
Main.java:181: error: class, interface, or enum expected
            for (int j = 0; j < m; j++) {
                            ^
Main.java:181: error: class, interface, or enum expected
            for (int j = 0; j < m; j++) {
                                   ^
Main.java:184: error: class, interface, or enum expected
                }
                ^
Main.java:187: error: class, interface, or enum expected
        }
        ^
100 errors
stdout
Standard output is empty