Problem Description
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 Inpu
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
思路:之前做过一个一模一样的题,但是那个题我直接用暴力深搜就能过,于是这一次做题时我采用了同样的方法结果TLE了,证明暴力过不了被卡了。
我又开始分析题意,发现所有可以相互引爆的点可以构成一个连通分量,于是我开始往强连通分量上靠拢,将一个连通分量缩小为一个点,这个点的引爆阈值为连通分量内阈值的最小值,最后将每个缩点过后的点阈值相加即可得到答案
AC代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
const int N=1010,M=1000010;
struct Bomb{
ll x,y,r,c;
}g[N];
int h[N],ne[M],e[M],idx;
int dfn[N],low[N],timestamp;
int id[N],scc_cnt,scc[N];
int din[N];
int stk[N],top;
bool in_stk[N];
int n;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
double getDist(int i,int j)
{
double x=g[i].x-g[j].x,y=g[i].y-g[j].y;
return sqrt(x*x+y*y);
}
void tarjan(int u)
{
dfn[u]=low[u]=++timestamp;
stk[++top]=u,in_stk[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!dfn[j])
{
tarjan(j);
low[u]=min(low[u],low[j]);
}
else if(in_stk[j])
low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u])
{
++scc_cnt;
scc[scc_cnt]=0x3f3f3f3f;
int y;
do{
y=stk[top--];
in_stk[y]=0;
id[y]=scc_cnt;
scc[scc_cnt]=min(scc[scc_cnt],(int)g[y].c);
}while(y!=u);
}
}
int main()
{
int t;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
timestamp=scc_cnt=top=idx=0;
memset(h,-1,sizeof h);
memset(dfn,0,sizeof dfn);
memset(din,0,sizeof din);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
g[i]={a,b,c,d};
}
for(int i=1;i<=n;i++)
for(int j=1;j<i;j++)
{
double d=getDist(i,j);
if(d<=g[i].r)
add(i,j);
if(d<=g[j].r)
add(j,i);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
for(int j=h[i];~j;j=ne[j])
{
int k=e[j];
int a=id[i],b=id[k];
if(a!=b)
din[b]++;
}
ll res=0;
for(int i=1;i<=scc_cnt;i++)
if(!din[i])
res+=scc[i];
printf("Case #%d: %d\n",k,res);
}
}