-----tarjan强连通分量缩点-hdu 5934 -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.
Input
First 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 xixi, yiyi, riri and cici, indicating the coordinate of ith bomb is (xi,yi)(xi,yi), exploding radius is riri and lighting-cost is cici.

Limits
- 1≤T≤201≤T≤20
- 1≤N≤10001≤N≤1000
- −108≤xi,yi,ri≤108−108≤xi,yi,ri≤108
- 1≤ci≤1041≤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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#define INF 1e16
using namespace std;

typedef long long ll;
const int maxn=1005;
int n,tot;
ll x[maxn],y[maxn],r[maxn],c[maxn];
ll Bcnt,Top,Index; /// 强连通分量的个数;
ll low[maxn], dfn[maxn],in0[maxn];
ll belong[maxn], stack[maxn],head[maxn];
bool instack[maxn];

struct node1
{
    ll to,next;
} G[maxn*maxn];

struct node2
{
    ll x,y,r,val;
}point[maxn];

void add(int u,int v)
{
    G[tot].to=v;
    G[tot].next=head[u];
    head[u]=tot++;
};

void Init_tarjan()
{
    Bcnt=Top=Index=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(head,-1,sizeof(head));
    memset(instack,0,sizeof(instack));
    memset(belong,0,sizeof(belong));
    memset(stack,0,sizeof(stack));
    memset(in0,0,sizeof(in0));
}
void Tarjan(int u)
{
    stack[Top++]= u;
    instack[u]= 1;
    low[u]= dfn[u]= ++Index;
    for(int i=head[u]; i!=-1; i=G[i].next)
    {
        int v= G[i].to;
        if( !dfn[v] )
        {
            Tarjan(v);
            low[u]= min( low[v], low[u] );
        }
        else if( instack[v] )
            low[u]= min( low[u], dfn[v] );
    }
    if( low[u]==dfn[u] )
    {
        ++Bcnt;
        int v;
        do
        {
            v= stack[--Top];
            instack[v]= 0;
            belong[v]= Bcnt;
        }
        while( u!=v );
    }
}

int main()
{
    int t;
    int ph=1;
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        Init_tarjan();
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%lld%lld%lld%lld",&point[i].x,&point[i].y,&point[i].r,&point[i].val);
        for(int i=0; i<n; i++)
            for(int j=i+1; j<n; j++)
            {
                ll dis=(point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y);
                if(dis<=point[i].r*point[i].r) add(i,j);
                if(dis<=point[j].r*point[j].r) add(j,i);
            }
        for(int i=0; i<n; i++)
        {
            if(dfn[i]==0)
                Tarjan(i);
        }
        for(int u=0; u<n; u++)
        {
            for(int i=head[u]; i!=-1; i=G[i].next)
            {
                int to=G[i].to;
                if(belong[u]!=belong[to])
                {
                    in0[belong[to]]++;
                }
            }
        }
        ll ans=0;
        for(int i=1; i<=Bcnt; i++)
        {
            if(in0[i]==0)
            {
                ll minn=INF;
                for(int u=0; u<n; u++)
                {
                    if(belong[u]==i)
                    {
                        minn=min(minn,point[u].val);
                    }
                }
                ans+=minn;
            }
        }
        printf("Case #%d: ",ph++);
        printf("%lld\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值