HDU 5934:Bomb(强连通缩点)

时间:2023-03-13 13:24:38

http://acm.hdu.edu.cn/showproblem.php?pid=5934

题意:有N个炸弹,每个炸弹有一个坐标,一个爆炸范围和一个爆炸花费,如果一个炸弹的爆炸范围内有另外的炸弹,那么如果该炸弹爆炸,就会引爆所有爆炸范围内的炸弹,求让所有炸弹爆炸的最小花费。

思路:重现的时候来不及做。先n^2的把每个炸弹爆炸范围内的炸弹都连一条有向边,然后再找强连通分量缩点,这样会形成多个DAG,然后对于每个DAG找一个入度为0的点,找这个入度为0的点里面耗费最小的去引爆,就可以了。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x7fffffff
struct node
{
int u, v, nxt;
}edge[];
struct P
{
long long x, y, r, c;
}p[N];
int belong[N], vis[N], head[N], tot, cnt, num, dfn[N], low[N], in[N], out[N];
stack<int> sta; void init() {
tot = cnt = num = ;
memset(dfn, , sizeof(dfn));
memset(belong, , sizeof(belong));
memset(vis, , sizeof(vis));
memset(head, -, sizeof(head));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
while(sta.size()) sta.pop();
} void add(int u, int v) {
edge[tot].u = u; edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} bool dis(int u, int v) {
double a = sqrt(((p[u].x - p[v].x) * (p[u].x - p[v].x) + (p[u].y - p[v].y) * (p[u].y - p[v].y)) * 1.0);
if(a <= p[u].r) return true;
return false;
} void tarjan(int u) {
vis[u] = ;
sta.push(u);
dfn[u] = low[u] = ++cnt;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(!dfn[v]) {
tarjan(v);
if(low[v] < low[u]) low[u] = low[v];
} else if(vis[v]) {
if(dfn[v] < low[u]) low[u] = dfn[v];
}
}
if(dfn[u] == low[u]) {
++num;
while(true) {
int v = sta.top(); sta.pop();
belong[v] = num; vis[v] = ;
if(v == u) break;
}
}
} int main()
{
int t, cas = ;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
init();
for(int i = ; i <= n; i++) scanf("%I64d%I64d%I64d%I64d", &p[i].x, &p[i].y, &p[i].r, &p[i].c);
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(i == j) continue;
if(dis(i, j)) add(i, j);
}
}
for(int i = ; i <= n; i++) if(!dfn[i]) tarjan(i);
for(int u = ; u <= n; u++) {
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(belong[u] != belong[v]) {
out[belong[u]]++;
in[belong[v]]++;
}
}
}
long long ans = ;
for(int i = ; i <= num; i++) {
if(in[i] == ) {
int mi = INF;
for(int j = ; j <= n; j++) {
if(belong[j] == i) {
if(p[j].c < mi) mi = p[j].c;
}
}
ans += mi;
}
}
printf("Case #%d: %d\n", cas++, ans);
}
return ;
}