using namespace std; int t,n; struct node{ LL x,y,r; int c; }a[maxn]; struct tarjan{ struct Edge{ int to,nxt; Edge(){} Edge(int t,int n):to(t),nxt(n){} }edge[maxm]; int head[maxn],tot,n; int low[maxn],DFN[maxn],stack[maxn],belong[maxn]; int index,top; int scc; int num[maxn]; bool instack[maxn]; inline void init(int n){ tot=0; this->n=n; memset(head,-1,sizeof(head)); } inline void addedge(int u,int v){ edge[tot]=Edge(v,head[u]); head[u]=tot++; } void Tarjan(int u){ int v; low[u]=DFN[u]=++index; stack[top++]=u; instack[u]=true; for(int i=head[u];~i;i=edge[i].nxt){ v=edge[i].to; if(!DFN[v]){ Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v] && low[u]>DFN[v]) low[u]=DFN[v]; } if(low[u]==DFN[u]){ scc++; do{ v=stack[--top]; instack[v]=false; belong[v]=scc; num[scc]++; }while(v!=u); } } int indeg[maxn],outdeg[maxn]; inline int solve(int n){ memset(DFN,0,sizeof(DFN)); memset(num,0,sizeof(num)); memset(instack,0,sizeof(instack)); index=scc=top=0; for(int i=1;i<=n;i++){ if(!DFN[i]) Tarjan(i); } memset(indeg,0,sizeof(indeg)); memset(outdeg,0,sizeof(outdeg)); for(int u=1;u<=n;u++){ for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(belong[u]!=belong[v]){ outdeg[belong[u]]++; indeg[belong[v]]++; } } } int ans=0; for(int i=1;i<=scc;i++){ if(indeg[i]==0){ int temp=inf; for(int j=1;j<=n;j++){ if(belong[j]==i) temp=min(temp,a[j].c); } ans+=temp; } } return ans; } }g; bool check(int i,int j){ if(a[i].r*a[i].r>=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)) return true; return false; } int Case=1; int main(){ // freopen("input.txt","r",stdin); scanf("%d",&t); while(t--){ scanf("%d",&n); g.init(n); for(int i=1;i<=n;i++) scanf("%lld%lld%lld%d",&a[i].x,&a[i].y,&a[i].r,&a[i].c); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) continue; if(check(i,j)) g.addedge(i,j); } } printf("Case #%d: %d\n",Case++,g.solve(n)); } return 0; }
|