HDU 5934:Boom——强连通分量+缩点

【题目描述】

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

【题目分析】
首先我们将问题转化为图论问题,我们按照能否引爆建图,我们肯定想点的是最前面的雷,即入度为0的雷。
可是有可能有环怎么办,我们将环用强连通分量缩成一个点,这样就变成了DAG图(有向无环图),然后找到入度为0 的点,点燃里面耗费最少的(强连通分量内部点燃一个其他的都会被引爆)
P S : PS: PS最好养成一个固定的做题习惯,比如下标是从0开始还是从1开始等。这里就因为前面从0开始后面从1开始wa了两发。我觉得最好养成从1开始的习惯,虽然sort什么麻烦一点,但是容易理解,而且图论中也不容易出错,不容易越界等。

【AC代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
const int MAXN=1e3+5;
const int MAXM=2e6+5;
struct boom
{
    int x,y,r,c;
}Boom[MAXN];
struct node
{
    int v,next;
}Edge[MAXM];
int head[MAXN],tot;
int DFN[MAXN],LOW[MAXN];
int color[MAXN],cnt;
bool vis[MAXN];
int idx;
int stack[MAXN],top;
int In[MAXN];
int n,ans;

void init()
{
    memset(head,0,sizeof(head)); tot=0;
    idx=0; memset(vis,0,sizeof(vis));
    memset(DFN,0,sizeof(DFN));
    memset(color,0,sizeof(color));
    cnt=0; top=0;
    memset(In,0,sizeof(In));
}

bool Close(int i,int j)
{
    ll xi=Boom[i].x; ll yi=Boom[i].y;
    ll xj=Boom[j].x; ll yj=Boom[j].y;
    ll dis=Boom[i].r;
    return dis*dis>=(xi-xj)*(xi-xj)+(yi-yj)*(yi-yj);
}

void AddEdge(int u,int v)
{
    tot++;
    Edge[tot].v=v; Edge[tot].next=head[u];
    head[u]=tot;
}

void read()
{
    int u,v;

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&Boom[i].x,&Boom[i].y,&Boom[i].r,&Boom[i].c);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) continue;
            if(Close(i,j)) AddEdge(i,j);
        }
    }
}

void Trajan(int x)
{
    int v,tmp;

    DFN[x]=LOW[x]=++idx;
    stack[++top]=x; vis[x]=true;
    for(int i=head[x];i;i=Edge[i].next)
    {
        v=Edge[i].v;
        if(!DFN[v])
        {
            Trajan(v);
            if(LOW[v]<LOW[x]) LOW[x]=LOW[v];
        }
        else if(vis[v] && LOW[v]<LOW[x])
        {
            LOW[x]=LOW[v];
        }
    }
    if(DFN[x]==LOW[x])
    {
        cnt++;
        do
        {
            tmp=stack[top--];
            vis[tmp]=false;
            color[tmp]=cnt;
        }while (tmp!=x);
    }
}

void solve()
{
    int v;

    for(int i=1;i<=n;i++)
    {
        if(!DFN[i])
        Trajan(i);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=Edge[j].next)
        {
            v=Edge[j].v;
            if(color[i]!=color[v])
            In[color[v]]++;
        }
    }
    ans=0;
    int minc;
    for(int i=1;i<=cnt;i++)
    {
        if(!In[i])
        {
            minc=INT_MAX;
            for(int j=1;j<=n;j++)
            {
                if(color[j]==i && Boom[j].c<minc)
                {
                    minc=Boom[j].c;
                }
            }
            ans+=minc;
            //printf("test: ans=%d minc=%d\n",ans,minc);
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int Case=1;Case<=T;Case++)
    {
        init();
        read();
        solve();
        printf("Case #%d: %d\n",Case,ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值