HDU5934Bomb(强连通缩点)

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1416    Accepted Submission(s): 462


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
1T20
1N1000
108xi,yi,ri108
1ci104
 

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


大佬的讲解:http://blog.csdn.net/mengxiang000000/article/details/52965154

题目大意:

给你N个炸弹,对应已知其坐标和爆炸范围,以及引爆这个炸弹需要的花费,对应如果引爆了炸弹a,没有引爆炸弹b,但是b炸弹在a炸弹的作用范围之内,那么b炸弹也会被引爆,问将所有炸弹都引爆需要的最小花费。


思路:


1、经典的最小点基的模型。我们首先O(n^2)预处理哪些炸弹可以被哪些炸弹引爆,得到一个有向图。


2、如果图中有有向环的话,我们可以将这一个有向环看成一个点,因为环内任意一个炸弹都能引爆这个环内所有的炸弹,所以我们使用Tarjan/Kosaraju之类的强连通算法缩点染色,使得图变成一个DAG(有向无环)图。


3、如果当前图变成了一个DAG图,那么度为0的节点一定是需要引爆的炸弹,因为这个节点中的炸弹不可能通过其他炸弹来引爆,只能通过直接引爆来达到引爆的目的,所以我们都将问题锁定在度为0的关键节点上来讨论,也就是所谓的最小点基问题。然后我们再简单分析一下,如果我们将所有度为0的节点都引爆了,那么度不为0的节点也一定会跟着被引爆,所以那么我们此时只需要将度为0的节点中找到一个对应的最小花费即可。


4、综上所述,我们Tarjan强联通缩点染色之后,找到度为0的节点,并且在其中找到花费最小的炸弹,累加即可。



#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct Bomb
{
    ll x, y, r, c;
}bomb[maxn];
struct Edge
{
    int to, nxt;
}edge[maxn*maxn];
bool vis[maxn];
int dfn[maxn], low[maxn], head[maxn], Stack[maxn], belong[maxn], in[maxn];
int cnt, tim, top, cut;
ll cost[maxn];
void addedge(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].nxt = head[u];
    head[u] = cnt++;
}
void init()
{
    memset(head, -1, sizeof(head));
    cnt = tim = top = cut = 0;
    memset(vis, false, sizeof(vis));
    memset(cost, inf, sizeof(cost));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(in, 0, sizeof(in));
    memset(belong, 0, sizeof(belong));
}
void Tarjan(int u)
{
    low[u] = dfn[u] = ++tim;
    Stack[top++] = u;
    vis[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].nxt)
    {
        int v = edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])
            low[u] = min(low[u], dfn[v]);
    }
    int v;
    if(low[u] == dfn[u])
    {
        cut++;
        do
        {
            v = Stack[--top];
            belong[v] = cut;
            cost[cut] = min(cost[cut], bomb[v].c);
            vis[v] = false;
        }
        while(u != v);
    }
}
int main()
{
    int T, n;
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++)
    {
        init();
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%I64d%I64d%I64d%I64d", &bomb[i].x, &bomb[i].y, &bomb[i].r, &bomb[i].c);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                if(i == j) continue;
                if((bomb[i].x-bomb[j].x)*(bomb[i].x-bomb[j].x)+(bomb[i].y-bomb[j].y)*(bomb[i].y-bomb[j].y) <= bomb[i].r*bomb[i].r)
                    addedge(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].nxt)
            {
                int v = edge[j].to;
                if(belong[i] != belong[v])
                    in[belong[v]]++;
            }
        ll ans = 0;
        for(int i = 1; i <= cut; i++)
            if(!in[i])
                ans += cost[i];
        printf("Case #%d: %lld\n", kase, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值