HDU-5934 Bomb(tarjan缩点+入度0最小和)

本文介绍了一种关于多个爆炸物之间的连锁引爆问题的算法解决方案。通过建立图模型,并利用Tarjan算法进行缩点处理,最终找到引爆所有炸弹所需的最低成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                      Bomb

There are NN bombs needing exploding. 

Each bomb has three attributes: exploding radius riri, position (xi,yi)(xi,yi) and lighting-cost cici which means you need to pay cici 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.
InputFirst line contains an integer TT, which indicates the number of test cases. 

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

In the following NN lines, the ith line contains four intergers xixiyiyiriri and cici, indicating the coordinate of ith bomb is (xi,yi)(xi,yi), exploding radius is riri and lighting-cost is cici

Limits 
1T201≤T≤20 
1N10001≤N≤1000 
108xi,yi,ri108−108≤xi,yi,ri≤108 
1ci1041≤ci≤104OutputFor 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
题目大概意思:输入t组数据,每组第一行输入一个n 接下来输入一个坐标x,y以及半径r 和引爆代价cost

我们需要引爆所有的点。连锁的点能不消耗代价引爆。然后计算引爆所有点的最少代价.
思路:先按能连锁的点建图,然后tarjan缩点,求出所有入度为0的点。然后更新最小代价。最后求和n个点的最少代价。

写了接近一个小时,开始数组开小T了两次,然后因为scc_cnt忘记置0WA一次。题目总体还是比较容易 蒟蒻...哎

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair<int ,int >
#define se second
#define fr frist
#define LL long long int
const int maxn=3500+10;
const int INF = 0x3f3f3f3f3f;
int Next[maxn*maxn],Lext[maxn],To[maxn*maxn],dis[maxn];
int dfn[maxn],low[maxn],scc[maxn],instc[maxn],cost[maxn],in[maxn],stc[maxn];
int top,times,cnt,scc_cnt,n;
struct node
{
    int x,y,r;
} p[maxn];
void init()
{
    cnt=top=times=scc_cnt=0;
    mem(dfn,0);
    mem(low,0);
    mem(Lext,-1);
    mem(scc,0);
    mem(instc,0);
    mem(in,0);
    mem(stc,0);
    mem(cost,0);
    mem(dis,INF);
    mem(in,0);
}
void add(int u,int v)
{
    Next[++cnt]=Lext[u];
    Lext[u]=cnt;
    To[cnt]=v;
}
int dfs(int u)
{
    dfn[u]=low[u]=++times;
    stc[++top]=u;
    instc[u]=1;
    for(int i=Lext[u]; i!=-1; i=Next[i])
    {
        int v=To[i];
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instc[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scc_cnt++;
        while(true)
        {
            int x=stc[top--];
            scc[x]=scc_cnt;
            instc[x]=0;
            if(x==u) break;
        }
    }
}
void tarjan()
{
    int i;
    for(i=1; i<=n; i++)
        if(!dfn[i]) dfs(i);
        for(i=1;i<=n;i++)
        {
            for(int j=Lext[i];j!=-1;j=Next[j])
            {
                int v=To[j];
                if(scc[v]!=scc[i])
                    in[scc[v]]++;
            }
        }
        for(i=1;i<=n;i++)
        {
            if(in[scc[i]]==0)
                dis[scc[i]]=min(dis[scc[i]],cost[i]);

        }
        int output=0;
        for(i=1;i<=scc_cnt;i++)
        {
            if(in[i]==0)
                output+=dis[i];
        }
        printf("%d\n",output);

}
int main()
{
    int t,k=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d %d %d",&p[i].x,&p[i].y,&p[i].r,&cost[i]);
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                LL X=p[i].x-p[j].x;
                LL Y=p[i].y-p[j].y;
                LL R=p[i].r;
                if(X*X+Y*Y<=R*R)
                {
                    //cout<<i<<"  "<<j<<endl;
                    add(i,j);
                }
            }
            printf("Case #%d: ",k++);
            tarjan();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值