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 133 134 135 136
| #include<bits/stdc++.h> using namespace std; #define ll long long const int INF = 0x3f3f3f3f; const ll inf = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 10; const ll mod = 1000000007; int T; struct E { int from, to, cp; ll v; int rev; E() {} E(int f, int t, int cp, ll v, int rev) :from(f), to(t), cp(cp), v(v), rev(rev) {} }; typedef pair<ll, ll>pii; ll ans[100]; struct MCMF { int n, m, s, t; vector<E> edges; vector<int> G[N]; bool inq[N]; ll d[N]; int p[N]; int a[N]; void init(int _n, int _s, int _t) { n = _n; s = _s; t = _t; for (int i = 0; i < n; i++) G[i].clear(); edges.clear(); m = 0; }
void addedge(int from, int to, int cap, ll cost) { edges.push_back(E(from, to, cap, cost, 0)); edges.push_back(E(to, from, 0, -cost, 1)); G[from].push_back(m++); G[to].push_back(m++); }
bool BellmanFord(int &flow, ll &cost) { for (int i = 0; i < n; i++) d[i] = inf; memset(inq, 0, sizeof inq); d[s] = 0, a[s] = INF, inq[s] = true; queue<int> Q; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = false; for (int& idx : G[u]) { E &e = edges[idx]; if (e.cp && d[e.to] > d[u] + e.v) { d[e.to] = d[u] + e.v; p[e.to] = idx; a[e.to] = min(a[u], e.cp); if (!inq[e.to]) { Q.push(e.to); inq[e.to] = true; } } } } if (d[t] == inf) return false; flow += a[t]; cost += a[t] * d[t]; int u = t; while (u != s) { edges[p[u]].cp -= a[t]; edges[p[u] ^ 1].cp += a[t]; u = edges[p[u]].from; } return true; }
pii go(int t) { int flow = 0; ll cost = 0ll; for (int i = 1; i <= t; i++) { BellmanFord(flow, cost); ans[i] = cost; } return pii(flow, cost); } } MM; int n; ll m; ll a[100], b[100], c[100]; vector<pii>vc[100]; vector<ll>vv; int main() { scanf("%d", &T); while (T--) { scanf("%d%lld", &n, &m); for (int i = 1; i <= n; i++)scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); vv.clear(); for (int i = 1; i <= n; i++) { vc[i].clear(); ll x = -b[i] / (2 * a[i]); if (x <= 1) { for (int j = 1; j <= n; j++)vc[i].push_back(pii(a[i] * j*j + b[i] * j + c[i], j)); } else if (x >= m) { for (ll j = m; j >= m - n + 1; j--) { vc[i].push_back(pii(a[i] * j*j + b[i] * j + c[i], j)); } } else { for (ll j = x; j >= max(1ll, x - n + 1); j--) { vc[i].push_back(pii(a[i] * j*j + b[i] * j + c[i], j)); } for (ll j = x + 1; j <= min(m, x + n); j++) { vc[i].push_back(pii(a[i] * j*j + b[i] * j + c[i], j)); } } sort(vc[i].begin(), vc[i].end()); for (int j = 0; j < n; j++) { vv.push_back(vc[i][j].second); } } sort(vv.begin(), vv.end()); vv.erase(unique(vv.begin(), vv.end()), vv.end()); int S = 0, T = n + (int)vv.size() + 1; MM.init(T + 1, S, T); for (int i = 1; i <= n; i++) { MM.addedge(S, i, 1, 0); for (int j = 0; j < n; j++) { pii p = vc[i][j]; int t = lower_bound(vv.begin(), vv.end(), p.second) - vv.begin() + 1; MM.addedge(i, n + t, 1, p.first); } } for (int i = 1; i <= (int)vv.size(); i++) { MM.addedge(n + i, T, 1, 0); } MM.go(n); for (int i = 1; i <= n; i++)printf("%lld%s", ans[i], i == n ? "\n" : " "); } return 0; }
|