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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <stack> #define maxn 110 #define ll long long #define INF 1000000000 using namespace std;
struct Edge { int to, next, w; } e[1000000]; int c1, head[maxn]; inline void add_edge(int u, int v, int w) { e[c1].to = v; e[c1].w = w; e[c1].next = head[u]; head[u] = c1++; }
inline void Add_edge(int u, int v, int w) { add_edge(u, v, w); add_edge(v, u, 0); }
int dep[maxn], s, t; bool bfs() { queue<int> Q; Q.push(s); fill(dep, dep + maxn, 0); dep[s] = 1; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w; if (w > 0 && !dep[v]) { dep[v] = dep[u] + 1; Q.push(v); if (v == t) return 1; } } } return 0; }
int dfs(int u, int _w) { if (!_w || u == t) return _w; int s = 0; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w; if (w > 0 && dep[v] == dep[u] + 1) { int di = dfs(v, min(_w - s, w)); e[i].w -= di; e[i ^ 1].w += di; s += di; if (s == _w) break; } } if (s < _w) dep[u] = 0; return s; }
int dinic() { int mf = 0; while (bfs()) mf += dfs(s, INF); return mf; }
int n; int x[maxn], y[maxn], a[maxn], r[maxn]; int g[maxn][maxn], G[maxn][maxn];
int F(int x, int y) { return (x - y) * (x - y); }
int dfn[maxn], low[maxn], bl[maxn], scc, w[maxn], c2; bool ins[maxn]; stack<int> S; void tarjan(int u) { dfn[u] = low[u] = ++c2; ins[u] = 1; S.push(u); for (int v = 1; v <= n; ++v) { if (!g[u][v] || u == v) continue; if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (ins[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { int t; w[++scc] = 0; do { t = S.top(); S.pop(); w[scc] += a[t]; bl[t] = scc; } while (t != u); } }
void rebuild() { for (int i = 1; i <= scc; ++i) for (int j = 1; j <= scc; ++j) G[i][j] = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (g[i][j]) G[bl[i]][bl[j]] = 1; }
void work() { cin >> n; fill(head, head + n + 2, -1); c1 = c2 = scc = 0; for (int i = 1; i <= n; ++i) dfn[i] = low[i] = 0; for (int i = 1; i <= n; ++i) cin >> x[i] >> y[i] >> r[i] >> a[i]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (F(x[i], x[j]) + F(y[i], y[j]) <= r[i] * r[i]) g[i][j] = 1; else g[i][j] = 0; for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) g[i][j] |= g[i][k] & g[k][j]; for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i); rebuild(); s = 0; t = n + 1; int ans = 0; for (int i = 1; i <= scc; ++i) { if (w[i] < 0) { Add_edge(i, t, -w[i]); continue; } ans += w[i]; Add_edge(s, i, w[i]); for (int j = 1; j <= scc; ++j) if (w[j] < 0 && G[i][j]) Add_edge(i, j, INF); } cout << ans - dinic() << "\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T; cin >> T; while (T--) work(); return 0; }
|