Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3352 Accepted Submission(s): 1046
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 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
题意:有n个在二维平面上的炸弹,给你炸弹的坐标,引爆范围以及引爆的代价。如果一个炸弹爆炸,在其爆炸范围内的炸弹也会爆炸,求引爆所有炸弹的最小代价。
做法:枚举每个炸弹爆炸时会影响到的其他炸弹并建立单向边,tarjan缩点,求所有入度为0的强连通分量中的最小代价的和即为答案。
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1005;
const int inf=0x3f3f3f3f;
typedef long long ll;
struct node{
ll x,y,cost,r,id;
}e[1005];
struct ede{
int from,to,next;
}ee[1000005];
int head[maxn],dfn[maxn],low[maxn],cnt,now;
int belong[maxn],qiang,n,in[maxn];
ll minn[maxn];
stack<int> sa;
void init(){
mem(head,-1);mem(dfn,0);
mem(low,0); mem(belong,0);
mem(minn,inf);mem(in,0);
cnt=now=qiang=0;
while(!sa.empty()) sa.pop();
}
ll dis(int a,int b){
return (e[a].x-e[b].x)*(e[a].x-e[b].x)+(e[a].y-e[b].y)*(e[a].y-e[b].y);
}
void add(int u,int v){
ee[now].from=u,ee[now].to=v,ee[now].next=head[u],head[u]=now++;
}
void tarjan(int root){
dfn[root]=low[root]=++cnt;
sa.push(root);
for(int i=head[root];~i;i=ee[i].next){
int v=ee[i].to;
if(!dfn[v]){
tarjan(v);
low[root]=min(low[root],low[v]);
}
else if(!belong[v])low[root]=min(low[root],dfn[v]);
}
if(low[root]==dfn[root]){
qiang++;
while(1){
int x=sa.top();sa.pop();
belong[x]=qiang;
if(x==root)break;
}
}
}
int main(){
int t,cas=0;
cin>>t;
while(t--){
scanf("%d",&n);
init();
for(int i=1;i<=n;i++){
scanf("%lld%lld%lld%lld",&e[i].x,&e[i].y,&e[i].r,&e[i].cost);
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
ll di=dis(i,j);
if(di<=(ll)e[i].r*e[i].r) add(i,j);
if(di<=(ll)e[j].r*e[j].r) add(j,i);
}
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++){
int x=belong[i];
minn[x]=min(minn[x],e[i].cost);
}
for(int i=0;i<now;i++){
int u=belong[ee[i].from],v=belong[ee[i].to];
if(u!=v)
in[v]++;
}
ll ans=0;
for(int i=1;i<=qiang;i++)
if(!in[i]) ans+=minn[i];
printf("Case #%d: %lld\n",++cas,ans);
}
return 0;
}