2016年中国大学生程序设计竞赛(杭州)传送门:HDU - 5934 Bomb
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
题意
给出n个炸弹的坐标(x,y)、爆炸能辐射成一个圆的半径r和引爆该炸弹需要的代价c,且某一个炸弹爆炸后,无需要代价直接引爆在能辐射圆内的炸弹,问如何引爆所有炸弹需要的最小代价。
题解
第一看这题是一个计算几何,但是仔细一想这是一道强连通分量的图论题。
对n个圆,直接以圆心O(n2)O(n^2)O(n2)的方式直接建有向图,通过tarjon求出各点输入第i个强连通分量并维护每个强连通分量的代价c最小值。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int MAX=1e3+7;
const ll inf=0x3f3f3f3f3f;
struct Point{
ll x,y,r,w;
}cir[MAX];
vector<ll>g[MAX]; //存图
ll dis(Point a,Point b){
ll dx=a.x-b.x,dy=a.y-b.y;return dx*dx+dy*dy;
}
ll n,dfn[MAX],dfncnt,low[MAX],st[MAX],in_stack[MAX],top,cost[MAX];
int scc[MAX], sc; // 结点 i 所在 强连通分量scc 的编号
int siz[MAX]; // 强连通 i 的大小
void tarjan(int u){
st[++top]=u; dfn[u]=low[u]=++dfncnt;in_stack[u]=1;for(int v:g[u]){
if(!dfn[v]){
tarjan(v);low[u]=min(low[u],low[v]); }else if(in_stack[v])low[u]=min(low[u],dfn[v]);}if(dfn[u]==low[u]){
sc++;while(1){
ll now=st[top--];in_stack[now]=0;scc[now]=sc;siz[sc]++;cost[sc]=min(cost[sc],cir[now].w);if(now==u)break; }}
}
void init(){
for(int i=1;i<=n;i++)g[i].clear();dfncnt=top=sc=0;memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low));memset(in_stack,0,sizeof(in_stack));memset(cost,inf,sizeof(cost));
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);int _,cas=1;cin>>_;while(_--){
cin>>n; init();for(int i=1;i<=n;i++)cin>>cir[i].x>>cir[i].y>>cir[i].r>>cir[i].w;for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j)continue;if(dis(cir[i],cir[j])<=cir[i].r*cir[i].r)g[i].push_back(j);}} for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);ll ans=0;vector<int>edge(n+1,0);for(int i=1;i<=n;i++){
for(auto x:g[i])if(scc[x]!=scc[i])edge[scc[x]]++;}for(int i=1;i<=sc;i++)if(!edge[i])ans+=cost[i];cout<<"Case #"<<cas++<<": "<<ans<<endl;}return 0;
}