HDU5934 Bomb 【强联通分量】

5 篇文章 0 订阅
4 篇文章 0 订阅
Bomb

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


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
- 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


Source
2016年中国大学生程序设计竞赛(杭州)

如果炸弹a能引爆炸弹b , 就在连一条a指向b的边(a->b)
于是问题就转化为 , 求选择若干个点 使得这些点能到达其他任何点 的最小花销

显然,入度为0的点 必然不能被其他点到达 只能选了
入度!=0时,有2种情况 , ①:该点能被一个入度为0的点到达
②:该点 能被一个环上的点 达到

对①:这个点不需要处理,对② :需要选择这个环中,费用最小的点
显然这个环可以通过强联通分量求出 缩点后的新点费用 为环中点费用的最小值
而缩点之后 不存在环 , 就只需要处理①情况 即将所有入度为0的点费用加起来

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<string.h>
#include<math.h>
#include<list>

using namespace std;

#define ll long long
#define pii pair<int,int>
const int inf = 1e9 + 7;

const int N=1000+5;
const int M=N*N+5;

struct Edge{
    int fr,to,next,nextRev;
}edge[M];

int head[N],headRev[N];//原图 反向边的图

void addEdge(int k,int u,int v){
    edge[k].fr=u;
    edge[k].to=v;
    edge[k].next=head[u];
    edge[k].nextRev=headRev[v];
    head[u]=headRev[v]=k;
}
int visited[N];//dfs标记
int num[N];//dfs1遍历的顺序
int scc[N];//scc[i]=第i个点属于第scc[i]个强连通分量
int dfs1(int cur,int&sig){
    visited[cur]=true;
    for(int i=head[cur];i!=-1;i=edge[i].next){
        if(visited[edge[i].to]==false){
            dfs1(edge[i].to,sig);
        }
    }
    num[sig++]=cur;
    return 0;
}
int dfs2(int cur,int sig){
    visited[cur]=true;
    scc[cur]=sig;
    for(int i=headRev[cur];i!=-1;i=edge[i].nextRev){
        if(!visited[edge[i].fr]){
            dfs2(edge[i].fr,sig);
        }
    }
    return 0;
}
int kosaraju(int n){
    int sig=0;
    fill(visited,visited+n+1,false);
    for(int i=1;i<=n;++i){
        if(visited[i]==false){
            dfs1(i,sig);
        }
    }
    sig=1;
    fill(visited,visited+n+1,false);
    for(int i=n-1;i>=0;--i){
        int k=num[i];
        if(visited[k]==false){
            dfs2(k,sig++);
        }
    }
    return sig-1;
}

struct Bomb{
    int x,y,r,w;
}bomb[N];

int inDeg[N];
int minCost[N];//引爆第i个点(缩点后)的最小花销

int slove(int n){
    int nume=0;
    fill(head,head+n+1,-1);
    fill(headRev,headRev+n+1,-1);
    for(int i=1;i<=n;++i){//构图
        for(int j=1;j<=n;++j){
            if(i==j){
                continue;
            }
            ll dis=(ll)(bomb[i].x-bomb[j].x)*(bomb[i].x-bomb[j].x) + (ll)(bomb[i].y-bomb[j].y)*(bomb[i].y-bomb[j].y);
            if((ll)bomb[i].r*bomb[i].r>=dis){
                addEdge(nume++,i,j);
            }
        }
    }

    int nb = kosaraju(n);//缩点
    fill(inDeg,inDeg+n+1,0);
    for(int i=0;i<nume;++i){//计算入度
        int u=edge[i].fr,v=edge[i].to;
        if(scc[u]!=scc[v]){
            ++inDeg[scc[v]];
        }
    }

    fill(minCost,minCost+nb+1,inf);
    int ans=0;
    for(int i=1;i<=n;++i){
        minCost[scc[i]]=min(minCost[scc[i]],bomb[i].w);
    }
    for(int i=1;i<=nb;++i){//引爆入度为0的点
        if(inDeg[i]==0){
            ans+=minCost[i];
        }
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            scanf("%d%d%d%d",&bomb[i].x,&bomb[i].y,&bomb[i].r,&bomb[i].w);
        }
        printf("Case #%d: %d\n",t,slove(n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值