HDU 5934 强联通分量
Posted liylho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5934 强联通分量相关的知识,希望对你有一定的参考价值。
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.
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
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
思路:将给出的点之间连边,对每个强联通分量去最小值即可。
代码:
1 #include<bits/stdc++.h> 2 //#include<regex> 3 #define db double 4 #include<vector> 5 #define ll long long 6 #define vec vector<ll> 7 #define Mt vector<vec> 8 #define ci(x) scanf("%d",&x) 9 #define cd(x) scanf("%lf",&x) 10 #define cl(x) scanf("%lld",&x) 11 #define pi(x) printf("%d\n",x) 12 #define pd(x) printf("%f\n",x) 13 #define pl(x) printf("%lld\n",x) 14 #define MP make_pair 15 #define PB push_back 16 #define inf 0x3f3f3f3f3f3f3f3f 17 #define fr(i, a, b) for(int i=a;i<=b;i++) 18 const int N = 4e6 + 5; 19 const int mod = 1e9 + 7; 20 const int MOD = mod - 1; 21 const db eps = 1e-18; 22 const db PI = acos(-1.0); 23 using namespace std; 24 struct P { 25 int f, to, nxt; 26 } e[N]; 27 28 struct PP { 29 int x, y, r, c; 30 } a[N]; 31 int hea[3500]; 32 int n, cnt, sig, tt, cont; 33 int deg[3500], sta[3500], col[3500], vis[3500], low[3500], dfn[3500], need[3500], contz[3500]; 34 35 void add(int f, int to) {//建边 36 e[cont].to = to; 37 e[cont].f = f; 38 e[cont].nxt = hea[f]; 39 hea[f] = cont++; 40 } 41 42 void tarjan(int u) {//强联通分量 43 vis[u] = 1; 44 low[u] = dfn[u] = cnt++; 45 sta[++tt] = u; 46 for (int i = hea[u]; i != -1; i = e[i].nxt) { 47 int v = e[i].to; 48 if (vis[v] == 0) tarjan(v); 49 if (vis[v] == 1) low[u] = min(low[u], low[v]); 50 } 51 if (dfn[u] == low[u]) { 52 sig++; 53 do { 54 col[sta[tt]] = sig; 55 vis[sta[tt]] = -1; 56 } while (sta[tt--] != u); 57 } 58 } 59 60 void cal() { 61 cnt = 1; 62 sig = 0; 63 tt = -1; 64 memset(dfn, 0, sizeof(dfn));memset(col, 0, sizeof(col));memset(vis, 0, sizeof(vis)); 65 memset(sta, 0, sizeof(sta));memset(low, 0, sizeof(low));memset(deg, 0, sizeof(deg)); 66 memset(contz, 0x3f3f3f3f, sizeof(contz)); 67 for (int i = 0; i < n; i++) if (!vis[i]) tarjan(i); 68 for (int i = 0; i < cont; i++) { 69 int u = e[i].f; 70 int v = e[i].to; 71 if (col[u] != col[v]) deg[col[v]]++; 72 } 73 for (int i = 0; i < n; i++) if (!deg[col[i]]) contz[col[i]] = min(contz[col[i]], a[i].c); 74 int ans = 0; 75 76 for (int i = 1; i <= sig; i++) if (!deg[i]) ans += contz[i]; 77 pi(ans); 78 } 79 80 int main() { 81 int t; 82 ci(t); 83 for (int ii = 1; ii <= t; ii++) { 84 ci(n); 85 for (int i = 0; i < n; i++) scanf("%d%d%d%d", &a[i].x, &a[i].y, &a[i].r, &a[i].c); 86 cont = 0; 87 memset(hea, -1, sizeof(hea)); 88 for (int i = 0; i < n; i++) { 89 for (int j = 0; j < n; j++) { 90 ll tmp = a[i].x - a[j].x; 91 ll tmp2 = a[i].y - a[j].y; 92 ll d1 = tmp * tmp + tmp2 * tmp2; 93 ll d2 = 1ll * a[i].r * a[i].r; 94 if (d1 <= d2) add(i, j); 95 } 96 } 97 printf("Case #%d: ", ii); 98 cal(); 99 } 100 return 0; 101 }
以上是关于HDU 5934 强联通分量的主要内容,如果未能解决你的问题,请参考以下文章