HDU - 5934 tarjan(强连通分量+缩点)

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

tarjan缩点,把属于强连通分量的点都缩成一个点。这里不用重新建图,只用in数组记一下缩完的点的度就行,度数是0就从该强连通分量中找到一个最小的花费。
(PS:WA了一晚上,最后坤神看出来是没加long long 的事,难受啊。以后记住了,逢乘必加long long。WTF!!)

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#define LL long long
using namespace std;
const int maxn=1e3+10;
int dfn[maxn],low[maxn],sta[maxn],vis[maxn],top,cnt; 
int in[maxn];
int color[maxn],sum;// 染色数组用来缩点,此题用不到 
//dfn[i]表示i是第几个搜到的,cnt用来计数编号 
//low[i]表示i及i的子孙中dfn的最小值
//sta表示栈数组,放的是当前强连通分量说包括的点
//vis[i]表示i是否在栈中 
vector <int> ve[maxn];//存图 
struct node
{
	int x,y,r,c;	
}bs[maxn]; 

void tarjan(int u)
{
   dfn[u]=low[u]=++cnt;
   sta[++top]=u;
   vis[u]=1;
   for(int i=0;i<ve[u].size();i++)
   {
   	  int v=ve[u][i];
   	  if(!dfn[v])
   	  {
   	     tarjan(v);	
   	   	 low[u]=min(low[u],low[v]);
	  }
	  else if(vis[v])
	    low[u]=min(low[u],low[v]);
   }
   if(dfn[u]==low[u])
   {
   	   color[u]=++sum;
   	   while(sta[top]!=u)
   	   {
   	   	 color[sta[top]]=sum;
   	     vis[sta[top--]]=0;	
	   }
	   vis[sta[top--]]=0;
   }
} 
int can(int a,int b)
{
	LL dis=(LL)(bs[a].x-bs[b].x)*(bs[a].x-bs[b].x)+(LL)(bs[a].y-bs[b].y)*(bs[a].y-bs[b].y);
	if(dis<=(LL)abs(bs[a].r)*(LL)abs(bs[a].r)) 
	   return 1;
	else 
	  return 0;
}
int main(void)
{
	int t;
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++)
	{
		int n;
		top=0;
		cnt=0;
		sum=0;
		scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
        	scanf("%d%d%d%d",&bs[i].x,&bs[i].y,&bs[i].r,&bs[i].c);
		    dfn[i]=0;
		    low[i]=0;
		    vis[i]=0;
			in[i]=0; 
		    ve[i].clear();
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(i==j) continue;
				if(can(i,j))
				{
					ve[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<ve[i].size();j++)
			{
				int to=ve[i][j];
				if(color[i]==color[to])
				   continue;
				in[color[to]]++;	
			}
		}
		int ans=0;
		for(int i=1;i<=sum;i++)
		{
			if(in[i]==0)
			{
				int cmin=1e4+10;
				for(int j=1;j<=n;j++)
				{
					if(color[j]==i)
					{
						cmin=min(cmin,bs[j].c);	
					}	
				}
				ans+=cmin;
			}
		}
	    printf("Case #%d: %d\n",cas,ans);			
	}

	return 0;
}

posted @ 2019-03-15 16:55  鸟人呀  阅读(233)  评论(0编辑  收藏  举报