http://acm.hdu.edu.cn/showproblem.php?pid=5934
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 Input1
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
题目大意:有
n
n
n个炸弹需要引爆,给出每个炸弹所在的位置
(
x
,
y
)
(x,y)
(x,y),爆炸的半径
r
r
r,以及引爆所需要的花费
c
c
c,当一个炸弹爆炸时,若另外一个炸弹与该炸弹的距离
<
=
r
<=r
<=r(
r
r
r为爆炸的炸弹的半径),那么这个炸弹也会被引爆。求把炸弹都引爆的最小花费。
思路:如果炸弹
i
i
i可以引爆炸弹
j
j
j,那么连一条有向边:
i
→
j
i\rightarrow j
i→j,最终我们可以得到一张有向图。对于图中的每一个强连通分量来说,只要支付这个强连通分量中最小的那个
c
c
c就可以把这堆炸弹都引爆,相当于把强联通分量当成一个点来操作,那不就是缩点操作嘛。在缩点得到的新图中,把入度为
0
0
0的点都引爆就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=1005;
const int maxm=1e6+5;
struct edge
{
int to,nxt;
}Edge[maxm];
struct point
{
ll x,y,r;
point(ll xx=0,ll yy=0,ll rr=0)
{
x=xx,y=yy,r=rr;
}
point operator - (const point &a)const
{
return point(x-a.x,y-a.y);
}
ll dis()
{
return x*x+y*y;
}
};
point p[maxn];
int head[maxn],dfn[maxn],low[maxn],Stack[maxn],ins[maxn],id[maxn],a[maxn];
int ans[maxn],ind[maxn];
int n,tot,num,top,cnt;
void addedge(int x,int y)
{
Edge[++tot].to=y,Edge[tot].nxt=head[x],head[x]=tot;
}
void tarjan(int x)
{
dfn[x]=low[x]=++num;
Stack[++top]=x,ins[x]=1;
int y;
for(int i=head[x];i;i=Edge[i].nxt)
{
y=Edge[i].to;
if(!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(ins[y])
low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x])
{
++cnt;
ans[cnt]=1e6;
do
{
y=Stack[top--],ins[y]=0;
id[y]=cnt,ans[cnt]=min(ans[cnt],a[y]);
}while(x!=y);
}
}
int main()
{
int t;
scanf("%d",&t);
ll dis;
int times=0;
while(t--)
{
top=cnt=num=tot=0;
memset(head,0,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(ind,0,sizeof(ind));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld%d",&p[i].x,&p[i].y,&p[i].r,&a[i]);
p[i].r*=p[i].r;
for(int j=i-1;j>=1;j--)
{
ll dis=(p[i]-p[j]).dis();
if(dis<=p[i].r)
addedge(i,j);
if(dis<=p[j].r)
addedge(j,i);
}
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
int y;
for(int i=1;i<=n;i++)
{
for(int j=head[i];j;j=Edge[j].nxt)
{
y=Edge[j].to;
if(id[i]!=id[y])
ind[id[y]]++;
}
}
ll re=0;
for(int i=1;i<=cnt;i++)
if(ind[i]==0)
re+=ans[i];
printf("Case #%d: %lld\n",++times,re);
}
return 0;
}