【题目描述】
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的雷。
可是有可能有环怎么办,我们将环用强连通分量缩成一个点,这样就变成了DAG图(有向无环图),然后找到入度为0 的点,点燃里面耗费最少的(强连通分量内部点燃一个其他的都会被引爆)
P
S
:
PS:
PS:最好养成一个固定的做题习惯,比如下标是从0开始还是从1开始等。这里就因为前面从0开始后面从1开始wa了两发。我觉得最好养成从1开始的习惯,虽然sort什么麻烦一点,但是容易理解,而且图论中也不容易出错,不容易越界等。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int MAXN=1e3+5;
const int MAXM=2e6+5;
struct boom
{
int x,y,r,c;
}Boom[MAXN];
struct node
{
int v,next;
}Edge[MAXM];
int head[MAXN],tot;
int DFN[MAXN],LOW[MAXN];
int color[MAXN],cnt;
bool vis[MAXN];
int idx;
int stack[MAXN],top;
int In[MAXN];
int n,ans;
void init()
{
memset(head,0,sizeof(head)); tot=0;
idx=0; memset(vis,0,sizeof(vis));
memset(DFN,0,sizeof(DFN));
memset(color,0,sizeof(color));
cnt=0; top=0;
memset(In,0,sizeof(In));
}
bool Close(int i,int j)
{
ll xi=Boom[i].x; ll yi=Boom[i].y;
ll xj=Boom[j].x; ll yj=Boom[j].y;
ll dis=Boom[i].r;
return dis*dis>=(xi-xj)*(xi-xj)+(yi-yj)*(yi-yj);
}
void AddEdge(int u,int v)
{
tot++;
Edge[tot].v=v; Edge[tot].next=head[u];
head[u]=tot;
}
void read()
{
int u,v;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&Boom[i].x,&Boom[i].y,&Boom[i].r,&Boom[i].c);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(Close(i,j)) AddEdge(i,j);
}
}
}
void Trajan(int x)
{
int v,tmp;
DFN[x]=LOW[x]=++idx;
stack[++top]=x; vis[x]=true;
for(int i=head[x];i;i=Edge[i].next)
{
v=Edge[i].v;
if(!DFN[v])
{
Trajan(v);
if(LOW[v]<LOW[x]) LOW[x]=LOW[v];
}
else if(vis[v] && LOW[v]<LOW[x])
{
LOW[x]=LOW[v];
}
}
if(DFN[x]==LOW[x])
{
cnt++;
do
{
tmp=stack[top--];
vis[tmp]=false;
color[tmp]=cnt;
}while (tmp!=x);
}
}
void solve()
{
int v;
for(int i=1;i<=n;i++)
{
if(!DFN[i])
Trajan(i);
}
for(int i=1;i<=n;i++)
{
for(int j=head[i];j;j=Edge[j].next)
{
v=Edge[j].v;
if(color[i]!=color[v])
In[color[v]]++;
}
}
ans=0;
int minc;
for(int i=1;i<=cnt;i++)
{
if(!In[i])
{
minc=INT_MAX;
for(int j=1;j<=n;j++)
{
if(color[j]==i && Boom[j].c<minc)
{
minc=Boom[j].c;
}
}
ans+=minc;
//printf("test: ans=%d minc=%d\n",ans,minc);
}
}
}
int main()
{
int T;
scanf("%d",&T);
for(int Case=1;Case<=T;Case++)
{
init();
read();
solve();
printf("Case #%d: %d\n",Case,ans);
}
return 0;
}