hdu 5934 Bomb (强联通分量缩点)

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

 

 

Source

2016年中国大学生程序设计竞赛(杭州)

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6602 6601 6600 6599 6598 

 

题目大意:给出n个炸弹的坐标和每个炸弹的爆炸半径以及点燃这个炸弹的花费,求如何花费最少引爆所有的炸弹。

解题思路:由于在被点燃炸弹的范围内的炸弹也会被引爆,可以想到连有向边。可以发现,如果在同一个强联通分量内

的点只要引爆一个就可以了,所以说同一个联通分量内的点我们取它的花费最小的。但是,题目所形成的图中的点可能

不在一个联通分量内,也不一定联通,所以我们将原图缩点后是一个有向无环图。然后就发现,所有入度为0的点都是需要被点燃的。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define sca(x) scanf("%d",&x)
#define pb(x) push_back(x)

const int N = 2005;
struct node
{
    LL x,y,r,c;
} a[N];
vector<int>V[N];
int in[N];

void read(int n)
{
    for(int i=1; i<=n; i++)
    scanf("%lld%lld%lld%lld",&a[i].x,&a[i].y,&a[i].r,&a[i].c);
}

bool judge(int i,int j)
{
    LL del=1LL*(a[j].x-a[i].x)*(a[j].x-a[i].x)+(a[j].y-a[i].y)*(a[j].y-a[i].y);
    LL tmp=1LL*a[i].r*a[i].r;
    if(del<=tmp) return true;
    return false;
}

int s;
int vis[N];
vector<int>G[N];
int bel[N];
int low[N],df[N];
int clo;
stack<int>S;

void tarjan(int u)
{
    low[u]=df[u]=++clo;
    S.push(u);
    for(int i=0;i<V[u].size();i++)
    {
        int v=V[u][i];
        if(!df[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!bel[v])
        {
            low[u]=min(low[u],df[v]);
        }
    }
    if(low[u]==df[u])
    {
        int tmp;
        ++s;
        do
        {
            tmp=S.top();
            S.pop();
            bel[tmp]=s;
            G[s].pb(tmp);
        }while(tmp!=u);
    }
}

void solve(int n)
{
    for(int i=1;i<=n;i++)
    {
        if(!df[i])
        {
            tarjan(i);
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<V[i].size();j++)
        {
            if(bel[i]!=bel[V[i][j]])
            {
                in[bel[V[i][j]]]++;
            }
        }
    }
    LL ans=0;
    for(int i=1;i<=s;i++)
    {
        if(in[i]==0)
        {
            LL mini=0x3f3f3f3f;
            for(int j=0;j<G[i].size();j++)mini=min(mini,a[G[i][j]].c);
            ans+=mini;
        }
    }
    cout<<ans<<endl;
}

void init(int n)
{
    s=0;clo=0;
    while(!S.empty())S.pop();
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(df,0,sizeof(df));
    memset(in,0,sizeof(in));
    memset(bel,0,sizeof(bel));
    for(int i=1;i<=n;i++)G[i].clear(),V[i].clear();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i!=j&&judge(i,j)){
                V[i].pb(j);
            }
}
int main()
{
    int t;
    sca(t);
    int cas=1;
    while(t--)
    {
        int n;
        sca(n);
        read(n);
        init(n);
        printf("Case #%d: ",cas++);
        solve(n);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值