bomb

There are  N N bombs needing exploding. 

Each bomb has three attributes: exploding radius  ri ri, position  (xi,yi) (xi,yi) and lighting-cost  ci ci which means you need to pay  ci 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 T, which indicates the number of test cases. 

Every test case begins with an integers  N N, which indicates the numbers of bombs. 

In the following  N N lines, the ith line contains four intergers  xi xi yi yi ri ri and  ci ci, indicating the coordinate of ith bomb is  (xi,yi) (xi,yi), exploding radius is  ri ri and lighting-cost is  ci ci

Limits 
1T20 1≤T≤20 
1N1000 1≤N≤1000 
108xi,yi,ri108 −108≤xi,yi,ri≤108 
1ci104 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


题意:给若干炸弹,给你炸弹的坐标,炸弹爆炸的范围和引爆炸弹的代价,让你求出引爆所有的炸弹最少的代价


强联通分量tarjin

首先将某一炸弹爆炸可以引爆其他炸弹,建图,然后跑tarjin

代码如下:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>

using namespace std;
typedef long long ll;

const int N=1e3+5;
const int INF=1e6+8;
struct Edge{
    int to,next;
}edge[N*N];
int head[N],dfn[N],low[N],instack[N],Stack[N],in[N],belong[N];
int num,top,dfs_time,k;
ll x[N],y[N],r[N],c[N];
void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(in,0,sizeof(in));
    memset(instack,0,sizeof(instack));
    num=0,k=0,top=0,dfs_time=0;
}
void add(int i,int j)
{
    edge[k].to=j;
    edge[k].next=head[i];
    head[i]=k++;
}
void tarjan(int start)
{
    dfn[start]=low[start]=++dfs_time;
    Stack[top++]=start;
    instack[start]=1;
    for(int i=head[start];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(!dfn[to])
        {
            tarjan(to);
            low[start]=min(low[start],low[to]);
        }
        else if(instack[to])
            low[start]=min(low[start],dfn[to]);
    }
    if(low[start]==dfn[start])
    {
        int to;
        num++;
        do{
            to=Stack[--top];
            instack[to]=0;
            belong[to]=num;
        }while(start!=to);
    }
}
int main()
{
    int t,n,count=1;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lld%lld%lld%lld",&x[i],&y[i],&r[i],&c[i]);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j)
                    if(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))<=r[i])//i能引爆j
                        add(i,j);
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                tarjan(i);
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=-1;j=edge[j].next)
            {
                int to=edge[j].to;
                if(belong[i]==belong[to]) continue;
                in[belong[to]]++;
            }
        }
        ll minn1=0;
        for(int i=1;i<=num;i++)
        {
            if(!in[i])
            {
                ll minn=INF;
                for(int j=1;j<=n;j++)
                    if(belong[j]==i)
                        minn=min(minn,c[j]);
                minn1+=minn;
            }
        }
        printf("Case #%d: %lld\n",count++,minn1);

    }
    return 0;
}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值