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,问你将所以的炸弹引爆最小的花费。
对于这一题,我们先求出强连通分量,然后把他们缩成一个点,然后再求这个图的每个点的入度,引爆所有入度为0的点,就是最小的花费,注意这题要用long long ,
求强连通分量我用的是kosaraju算法,完整代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1010;
const int INF = 0x3f3f3f3f;
class Point{
public:
ll x,y,r;
int c;
void setdate(){
cin >> x >> y >> r >> c;
}
};
class Point q[MAX];
int N;
vector<int> G[MAX],RG[MAX];
vector<int> vs;
int cmp[MAX],value[MAX],InDegree[MAX];
void add(int u,int v){
G[u].push_back(v);
RG[v].push_back(u);
}
bool used[MAX];
void dfs(int v){
used[v] = true;
for(int i=0;i<G[v].size();++i){
if(!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k){
used[v] = true;
cmp[v] = k;
for(int i=0;i<RG[v].size();++i){
if(!used[RG[v][i]]) rdfs(RG[v][i],k);
}
}
ll solve(){
for(int i=1;i<=N;++i){
for(int j=1;j<=N;++j){
if(i != j){
ll xx = abs(q[i].x-q[j].x);
ll yy = abs(q[i].y-q[j].y);
ll dis = xx*xx + yy*yy;
if(dis <= q[i].r*q[i].r)
add(i,j);
}
}
}
// Debug();
memset(used,false,sizeof(used));
vs.clear();
for(int v=1;v<=N;++v)
if(!used[v]) dfs(v);
memset(used,false,sizeof(used));
int k = 0;
for(int i=vs.size()-1;i >=0;--i)
if(!used[vs[i]]) rdfs(vs[i],k++);
memset(value,INF,sizeof(value));
for(int i=1;i<=N;++i){
value[cmp[i]] = min(value[cmp[i]],q[i].c);
}
memset(InDegree,0,sizeof(InDegree));
for(int i=1;i<=N;++i){
for(int j=0;j<G[i].size();++j){
if(cmp[i] != cmp[G[i][j]]){
InDegree[cmp[G[i][j]]]++;
}
}
}
ll res = 0;
for(int i=0;i<k;++i){
if(!InDegree[i])
res += value[i];
}
return res;
}
int main(void){
// freopen("in.txt","r",stdin);
int T,Case = 0;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=1;i<=N;++i){
G[i].clear();
RG[i].clear();
}
for(int i=1;i<=N;++i)
q[i].setdate();
printf("Case #%d: %lld\n",++Case,solve());
}
return 0;
}
本文介绍了一种算法问题,即如何以最小的成本引爆一组相互关联的炸弹。通过计算每个炸弹的位置、爆炸范围和引爆成本,利用强连通分量的概念和kosaraju算法,文章详细解释了如何找到最优解决方案。

356

被折叠的 条评论
为什么被折叠?



