There are N bombs needing exploding.
Each bomb has three attributes: exploding radius ri, position (xi,yi) and lighting-cost ci which means you need to pay ci cost making it explode.
If a un-lighting bomb is in or on the border the exploding area of another exploding one, the un-lighting bomb also will explode.
Now you know the attributes of all bombs, please use the minimum cost to explode all bombs.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case begins with an integers N, which indicates the numbers of bombs.
In the following N lines, the ith line contains four intergers xi, yi, ri and ci, indicating the coordinate of ith bomb is (xi,yi), exploding radius is ri and lighting-cost is ci.
Limits
- 1≤T≤20
- 1≤N≤1000
- −108≤xi,yi,ri≤108
- 1≤ci≤104
Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the minimum cost.
Sample Input
1
5
0 0 1 5
1 1 1 6
0 1 1 7
3 0 2 10
5 0 1 4
Sample Output
Case #1: 15
找到入度为0的连通分量,并遍历一次找到每个联通分量中费用最小的即可
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#define N 1005
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
bool in[N];
ll x[N],y[N],r[N];
int c[N];
int s[N],dfn[N],low[N],degree[N],f[N];
int ans,top,num;
vector<int> g[N];
void tarjan(int u)
{
int v;s[++top]=u;in[u]=true;
dfn[u]=low[u]=++num;
for(int i=0;i<g[u].size();i++)
{
v=g[u][i];
if(!dfn[v])
{
tarjan(v);
if(low[v]<low[u])
low[u]=low[v];
}
else if(in[v] && dfn[v]<low[u])
low[u]=dfn[v];
}
if(dfn[u]==low[u])
{
ans++;
do
{
v=s[top];s[top]=0;top--;
f[v]=ans;
in[v]=false;
}while(v!=u);
}
}
int main()
{
int t;
scanf("%d",&t);
for(int tim=1;tim<=t;tim++)
{
memset(s,0,sizeof(s));
memset(in,false,sizeof(in));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(degree,0,sizeof(degree));
memset(f,0,sizeof(f));
ans=top=num=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
g[i].clear();
for(int i=1;i<=n;i++)
scanf("%lld%lld%lld%lld",x+i,y+i,r+i,c+i);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
{
if(1ll*(x[i]-x[j])*(x[i]-x[j])+1ll*(y[i]-y[j])*(y[i]-y[j])<=(1ll*r[i]*r[i]))
g[i].push_back(j);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
{
for(int j=0;j<g[i].size();j++)
{
int v=g[i][j];
if(f[i]!=f[v])
degree[f[v]]++;
}
}
ll sum=0;
for(int i=1;i<=ans;i++)
{
if(degree[i]==0)
{
int tmp=1e5;
for(int j=1;j<=n;j++)
{
if(f[j]==i)
{
tmp=min(tmp,c[j]);
}
}
sum+=tmp;
}
}
printf("Case #%d: %d\n",tim,sum);
}
}