#include <bits/stdc++.h> using namespace std; const int maxn = 1e3+10; typedef long long ll; struct Node{ ll x, y, r, c; }node[maxn]; const int INF = 1e9+10; int n, m; bool sqr(Node a, Node b){ ll r = a.r*a.r; ll dis = ((a.x-b.x)*(a.x-b.x))+(a.y-b.y)*(a.y-b.y); return r>=dis; } vector<int> G[maxn]; vector<int> rG[maxn]; vector<int> vs; ll cost[maxn]; int belong[maxn]; int k; bool vis[maxn]; void dfs(int u){ vis[u] = true; for(int i=0; i<G[u].size(); i++){ int v = G[u][i]; if(!vis[v]){ dfs(v); } } vs.push_back(u); } void rdfs(int u, int k){ vis[u] = true; cost[k] = min(cost[k], node[u].c); belong[u] = k; for(int i=0; i<rG[u].size(); i++){ int v = rG[u][i]; if(!vis[v]) rdfs(v, k); } } int scc(){ vs.clear(); memset(vis, 0, sizeof(vis)); for(int i=0; i<n; i++){ if(!vis[i]){ dfs(i); } } k=0; memset(vis, 0, sizeof(vis)); for(int i=vs.size()-1; i>=0; i--){ int u = vs[i]; if(!vis[u]) { cost[++k] = INF; rdfs(u, k); } } } int in[maxn]; int out[maxn]; vector<int> new_G[maxn]; void init(){ for(int i=0; i<maxn; i++){ G[i].clear(); rG[i].clear(); new_G[i].clear(); } memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); } int kase = 0; int main(){ int t; cin>>t; while(t--){ cin>>n; init(); for(int i=0; i<n; i++){ int x, y, r, c; cin>>x>>y>>r>>c; node[i].x = x, node[i].y = y; node[i].r = r, node[i].c = c; } for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(i == j) continue; if(sqr(node[i], node[j])){ G[i].push_back(j); rG[j].push_back(i); } } } int tot = scc(); for(int i=0; i<n; i++){ for(int j=0; j<G[i].size(); j++){ int v = G[i][j]; if(belong[i]!=belong[v]){ in[belong[v]]++; } } } int ans = 0; for(int i=1; i<=k; i++){ if(!in[i]) ans += cost[i]; } printf("Case #%d: %d\n", ++kase, ans); } return 0; }
|