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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章