HDU - 5934 Bomb(tarjan缩点)

HDU-5934 Bomb(tarjan缩点)

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个炸弹,每个炸弹都有一个座标(x,y)和一个爆炸半径 r 还有一个引爆这个炸弹的代价c
若一个炸弹被引爆 那么它的爆炸范围内的所有的炸弹都会被引爆。
问:最少需要多少代价,让所有的炸弹都引爆。

解题思路:

我们用n^2的复杂度建图 。如果A能引爆B 就在A B之间连一条有向边。

一个强连通分量里面的所有点,只要引爆一个,那么这个强连通分量里面的所有点都会被引爆。
那我们就用 tarjan缩点,然后重新建图。重新建图的结点的花费是这个强连通分量里面的花费最小的点。
然后在新图中找入度为0的点引爆。

AC代码:

#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int maxn = 1e3+10;
const int maxm = 1e6+10;
struct Node{
    ll x;
    ll y;
    ll r;
    ll c;
}a[maxn];
struct node{
    int to;
    int next;
}side[maxm];
int out[maxn];
int head[maxn],tot=0;
int DFN[maxn];
int LOW[maxn];
int ins[maxn];
stack<int> s;
int timing = 0;
int colornum[maxn];
int color[maxn];
int cnt = 0;
int in[maxn];
ll book[maxn];
int n;
void init(){
	memset(head,-1,sizeof(head));
	for(int i=0;i<=n;i++){
		DFN[i] = 0;
		LOW[i] = 0;
		ins[i] = 0;
		color[i] = 0;
		colornum[i] = 0;
		in[i] = 0;
		out[maxn] = 0;
		book[i] = 0x3f3f3f3f;
	}
	cnt = tot = timing = 0;
	while(s.size()) s.pop();
}
void add(int x,int y){
	side[tot].to = y;
	side[tot].next = head[x];
	head[x] = tot++;
}
void targan(int u){
	timing++;
	DFN[u] = LOW[u] =timing;
	s.push(u);
	ins[u] = 1;
	for(int i=head[u];i!=-1;i=side[i].next){
		int to = side[i].to;
		if(DFN[to]==0){
			targan(to);
			LOW[u] = min(LOW[u],LOW[to]);
		}else if(ins[to]==1){
			LOW[u] = min(LOW[u],DFN[to]);
		}
	}
	if(DFN[u]==LOW[u]){
		cnt++;//强连通分量的标号
		//遍历这个强连通分量重的点,都pop出来
		while(s.top()!=u){
			int tmp = s.top();
			s.pop();
			ins[tmp] = 0;//T出栈
			color[tmp] = cnt;
			colornum[cnt] ++;
		}
		s.pop();
		color[u] = cnt;
		colornum[cnt]++;
		ins[u] = 0;//T出栈
	}

}
ll fun(ll x1,ll y1,ll x2,ll y2){
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}

int main(){
    int t;
    cin>>t;
    int cas = 1;
    while(t--){
        scanf("%d",&n);
        init();
        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) continue;
                ll dis = fun(a[i].x,a[i].y,a[j].x,a[j].y);
                if((a[i].r)*(a[i].r)>=dis){
                    add(i,j);                    
                } 
            }
        }
        for(int i=1;i<=n;i++){
            if(!DFN[i]){
                targan(i);
            }
        } 
        for(int i=1;i<=n;i++){
        	int x = color[i];
        	book[x] = min(book[x],a[i].c);
        	for(int j=head[i];j!=-1;j=side[j].next){
        		int to =side[j].to;
        		int y = color[to];
        		if(x!=y){
        			out[x]++;
        			in[y]++;
				} 
			}
		}
		ll res = 0;
        for(int i=1;i<=cnt;i++){
        	if(in[i]==0){
        		res += book[i];
			}
		}
        printf("Case #%d: %lld\n",cas++,res);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章