HDU 5934 强联通分量

时间:2022-11-20 08:24:52

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1853    Accepted Submission(s): 608

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
 
Source

思路:将给出的点之间连边,对每个强联通分量去最小值即可。

代码:

 ```C++
#include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i, a, b) for(int i=a;i<=b;i++)
const int N = 4e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
struct P {
int f, to, nxt;
} e[N]; struct PP {
int x, y, r, c;
} a[N];
int hea[];
int n, cnt, sig, tt, cont;
int deg[], sta[], col[], vis[], low[], dfn[], need[], contz[]; void add(int f, int to) {//建边
e[cont].to = to;
e[cont].f = f;
e[cont].nxt = hea[f];
hea[f] = cont++;
} void tarjan(int u) {//强联通分量
vis[u] = ;
low[u] = dfn[u] = cnt++;
sta[++tt] = u;
for (int i = hea[u]; i != -; i = e[i].nxt) {
int v = e[i].to;
if (vis[v] == ) tarjan(v);
if (vis[v] == ) low[u] = min(low[u], low[v]);
}
if (dfn[u] == low[u]) {
sig++;
do {
col[sta[tt]] = sig;
vis[sta[tt]] = -;
} while (sta[tt--] != u);
}
} void cal() {
cnt = ;
sig = ;
tt = -;
memset(dfn, , sizeof(dfn));memset(col, , sizeof(col));memset(vis, , sizeof(vis));
memset(sta, , sizeof(sta));memset(low, , sizeof(low));memset(deg, , sizeof(deg));
memset(contz, 0x3f3f3f3f, sizeof(contz));
for (int i = ; i < n; i++) if (!vis[i]) tarjan(i);
for (int i = ; i < cont; i++) {
int u = e[i].f;
int v = e[i].to;
if (col[u] != col[v]) deg[col[v]]++;
}
for (int i = ; i < n; i++) if (!deg[col[i]]) contz[col[i]] = min(contz[col[i]], a[i].c);
int ans = ; for (int i = ; i <= sig; i++) if (!deg[i]) ans += contz[i];
pi(ans);
} int main() {
int t;
ci(t);
for (int ii = ; ii <= t; ii++) {
ci(n);
for (int i = ; i < n; i++) scanf("%d%d%d%d", &a[i].x, &a[i].y, &a[i].r, &a[i].c);
cont = ;
memset(hea, -, sizeof(hea));
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
ll tmp = a[i].x - a[j].x;
ll tmp2 = a[i].y - a[j].y;
ll d1 = tmp * tmp + tmp2 * tmp2;
ll d2 = 1ll * a[i].r * a[i].r;
if (d1 <= d2) add(i, j);
}
}
printf("Case #%d: ", ii);
cal();
}
return ;
} ```
 #include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i, a, b) for(int i=a;i<=b;i++)
const int N = 4e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
struct P {
int f, to, nxt;
} e[N]; struct PP {
int x, y, r, c;
} a[N];
int hea[];
int n, cnt, sig, tt, cont;
int deg[], sta[], col[], vis[], low[], dfn[], need[], contz[]; void add(int f, int to) {//建边
e[cont].to = to;
e[cont].f = f;
e[cont].nxt = hea[f];
hea[f] = cont++;
} void tarjan(int u) {//强联通分量
vis[u] = ;
low[u] = dfn[u] = cnt++;
sta[++tt] = u;
for (int i = hea[u]; i != -; i = e[i].nxt) {
int v = e[i].to;
if (vis[v] == ) tarjan(v);
if (vis[v] == ) low[u] = min(low[u], low[v]);
}
if (dfn[u] == low[u]) {
sig++;
do {
col[sta[tt]] = sig;
vis[sta[tt]] = -;
} while (sta[tt--] != u);
}
} void cal() {
cnt = ;
sig = ;
tt = -;
memset(dfn, , sizeof(dfn));memset(col, , sizeof(col));memset(vis, , sizeof(vis));
memset(sta, , sizeof(sta));memset(low, , sizeof(low));memset(deg, , sizeof(deg));
memset(contz, 0x3f3f3f3f, sizeof(contz));
for (int i = ; i < n; i++) if (!vis[i]) tarjan(i);
for (int i = ; i < cont; i++) {
int u = e[i].f;
int v = e[i].to;
if (col[u] != col[v]) deg[col[v]]++;
}
for (int i = ; i < n; i++) if (!deg[col[i]]) contz[col[i]] = min(contz[col[i]], a[i].c);
int ans = ; for (int i = ; i <= sig; i++) if (!deg[i]) ans += contz[i];
pi(ans);
} int main() {
int t;
ci(t);
for (int ii = ; ii <= t; ii++) {
ci(n);
for (int i = ; i < n; i++) scanf("%d%d%d%d", &a[i].x, &a[i].y, &a[i].r, &a[i].c);
cont = ;
memset(hea, -, sizeof(hea));
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
ll tmp = a[i].x - a[j].x;
ll tmp2 = a[i].y - a[j].y;
ll d1 = tmp * tmp + tmp2 * tmp2;
ll d2 = 1ll * a[i].r * a[i].r;
if (d1 <= d2) add(i, j);
}
}
printf("Case #%d: ", ii);
cal();
}
return ;
}