19暑假连通分量A

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的连通分量,并遍历一次找到每个联通分量中费用最小的即可
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#define N 1005
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
bool in[N];
ll x[N],y[N],r[N];
int c[N];
int s[N],dfn[N],low[N],degree[N],f[N];
int ans,top,num;
vector<int> g[N];
void tarjan(int u) 
{  
    int v;s[++top]=u;in[u]=true;  
    dfn[u]=low[u]=++num;  
    for(int i=0;i<g[u].size();i++) 
    {  
        v=g[u][i];  
        if(!dfn[v]) 
        {  
            tarjan(v);  
            if(low[v]<low[u])  
                low[u]=low[v];  
        }  
        else  if(in[v] && dfn[v]<low[u])  
                low[u]=dfn[v];  
    }  
    if(dfn[u]==low[u]) 
    {  
        ans++;  
        do 
        {  
            v=s[top];s[top]=0;top--;  
            f[v]=ans;  
            in[v]=false;  
        }while(v!=u);  
    }  
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int tim=1;tim<=t;tim++)
	{
		memset(s,0,sizeof(s));
		memset(in,false,sizeof(in));
		memset(dfn,0,sizeof(dfn)); 
		memset(low,0,sizeof(low));
		memset(degree,0,sizeof(degree));
		memset(f,0,sizeof(f));
		ans=top=num=0;
		int n;		
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		g[i].clear();
		for(int i=1;i<=n;i++)
		scanf("%lld%lld%lld%lld",x+i,y+i,r+i,c+i);
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		if(i!=j)
		{			
			if(1ll*(x[i]-x[j])*(x[i]-x[j])+1ll*(y[i]-y[j])*(y[i]-y[j])<=(1ll*r[i]*r[i]))
			g[i].push_back(j);
		}
		for(int i=1;i<=n;i++)
		if(!dfn[i])
		tarjan(i);
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<g[i].size();j++)
			{
				int v=g[i][j];
				if(f[i]!=f[v])
				degree[f[v]]++;
			}
		}
		ll sum=0;
		for(int i=1;i<=ans;i++)
        {
            if(degree[i]==0)
            {
                int tmp=1e5;
                for(int j=1;j<=n;j++)
                {
                    if(f[j]==i)
                    {
                        tmp=min(tmp,c[j]);
                    }
                }
                sum+=tmp;
            }
        }
		printf("Case #%d: %d\n",tim,sum);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值