hdu 5934 Bomb
#include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <vector> #include <cmath> using namespace std; #define ll long long const long long INF=1e9; int T,n,dfn[1010],scc[1010],low[1010],scc_cnt,dfs_clock; ll cost[1010],inx[1010]; struct boom{ll x,y,r,c;}a[1010]; stack<int> s; vector<int> G[1010]; ll dis(boom x,boom y){return (x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y);} void dfs(int u){ dfn[u]=low[u]=++dfs_clock; s.push(u); for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(!dfn[v]){ dfs(v); low[u]=min(low[u],low[v]); } else if(!scc[v])low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]){ ++scc_cnt; for(;;){ int x=s.top(); s.pop(); scc[x]=scc_cnt; if(x==u)break; } } } void find_scc(){ scc_cnt=dfs_clock=0; memset(dfn,0,sizeof(dfn)); memset(scc,0,sizeof(scc)); for(int i=1;i<=n;i++)if(!scc[i])dfs(i); } int main(){ scanf("%d",&T); for(int t=1;t<=T;t++){ scanf("%d",&n); for(int i=1;i<=n;i++)G[i].clear(); memset(inx,0,sizeof(inx)); for(int i=1;i<=n;i++)scanf("%lld%lld%lld%lld",&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 && dis(a[i],a[j])<=a[i].r*a[i].r)G[i].push_back(j); find_scc(); for(int i=1;i<=scc_cnt;i++)cost[i]=INF; for(int u=1;u<=n;u++){ cost[scc[u]]=min(cost[scc[u]],a[u].c); for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(scc[u]!=scc[v])inx[scc[v]]++; } } ll ans=0; for(int i=1;i<=scc_cnt;i++)if(!inx[i])ans+=cost[i]; printf("Case #%d: %lld\n",t,ans); } return 0; }