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

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 Inpu

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

思路:之前做过一个一模一样的题,但是那个题我直接用暴力深搜就能过,于是这一次做题时我采用了同样的方法结果TLE了,证明暴力过不了被卡了。

我又开始分析题意,发现所有可以相互引爆的点可以构成一个连通分量,于是我开始往强连通分量上靠拢,将一个连通分量缩小为一个点,这个点的引爆阈值为连通分量内阈值的最小值,最后将每个缩点过后的点阈值相加即可得到答案

AC代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

#define ll long long
const int N=1010,M=1000010;

struct Bomb{
	ll x,y,r,c;
}g[N];
int h[N],ne[M],e[M],idx;
int dfn[N],low[N],timestamp;
int id[N],scc_cnt,scc[N];
int din[N];
int stk[N],top;
bool in_stk[N];
int n;

void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

double getDist(int i,int j)
{
	double x=g[i].x-g[j].x,y=g[i].y-g[j].y;
	return sqrt(x*x+y*y);
}

void tarjan(int u)
{
	dfn[u]=low[u]=++timestamp;
	stk[++top]=u,in_stk[u]=1;
	
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(!dfn[j])
		{
			tarjan(j);
			low[u]=min(low[u],low[j]);
		}
		else if(in_stk[j])
			low[u]=min(low[u],dfn[j]);
	}
	
	if(dfn[u]==low[u])
	{
		++scc_cnt;
		scc[scc_cnt]=0x3f3f3f3f;
		int y;
		do{
			y=stk[top--];
			in_stk[y]=0;
			id[y]=scc_cnt;
			scc[scc_cnt]=min(scc[scc_cnt],(int)g[y].c);
		}while(y!=u);
	}
}

int main()
{
	int t;
	scanf("%d",&t);
	for(int k=1;k<=t;k++)
	{
		timestamp=scc_cnt=top=idx=0;
		memset(h,-1,sizeof h);
		memset(dfn,0,sizeof dfn);
		memset(din,0,sizeof din);
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			int a,b,c,d;
			scanf("%d%d%d%d",&a,&b,&c,&d);
			g[i]={a,b,c,d};
		}
		for(int i=1;i<=n;i++)
			for(int j=1;j<i;j++)
		{
			double d=getDist(i,j);
			if(d<=g[i].r)
				add(i,j);
			if(d<=g[j].r)
				add(j,i);
		}
		
		for(int i=1;i<=n;i++)
			if(!dfn[i])
				tarjan(i);
		
		for(int i=1;i<=n;i++)
			for(int j=h[i];~j;j=ne[j])
		{
			int k=e[j];
			int a=id[i],b=id[k];
			if(a!=b)
				din[b]++;
		}
		
		ll res=0;
		for(int i=1;i<=scc_cnt;i++)
			if(!din[i])
				res+=scc[i];
		
		printf("Case #%d: %d\n",k,res);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double.Qing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值