Bomb (hdu 5934 && 2016icpc杭州现场赛题目)
来源:互联网 发布:斗鱼刷人气软件安卓版 编辑:程序博客网 时间:2024/05/17 07:00
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 213 Accepted Submission(s): 89
Problem Description
There are N bombs needing exploding.
Each bomb has three attributes: exploding radiusri , 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
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 integersN , which indicates the numbers of bombs.
In the followingN 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
Every test case begins with an integers
In the following
Limits
1<=T<=20
1<=N<=1000
-10^8 <= xi, yi, ri <= 10^8
1<=ci<=10^4
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
150 0 1 51 1 1 60 1 1 73 0 2 105 0 1 4
Sample Output
Case #1: 15
Source
2016年中国大学生程序设计竞赛(杭州)
这是现场赛的我们卡的这道题,两个多小时到最后也没弄出来,唉!!结束后当场问了其他学校的巨巨才知道是一道强联通题,才知道有强连通这种东东,前天打了杭电的重赛还是卡了这道题,我还是在金巨的循循善导在才学会了的。其实我的当时在现场赛写的代码我到现在也想不出错在了哪,最后贴出, 希望有大神找一下我的当时的代码错哪了!
题目就是给你一堆炸弹, 每个炸弹都给出了位置,爆炸的范围(这个数竟然可能有负的也是对出题人无语了)与及这个炸弹爆炸的花费,当一个炸弹爆炸时这个炸弹爆炸范围内的炸弹都会爆炸。首先枚举每个炸弹然后这个炸弹爆炸范围内的炸弹都建立一条有向边,因为每个炸弹的爆炸范围不同,如K炸弹的爆炸范围较大,当K爆炸时能使P爆炸,但是P爆炸范围较小,P爆炸不能一定能使K爆炸。建完有向图后用Tarjan算法进行强连通缩点(如果不会建议先去自己学习,因为死磕是行不通的。我当时也是看不懂别人的博客较为纠结,然后听从金巨的建议找来了“大白书”(算法竞赛入门经典)学习这种算法,网上相关的博客都是说得糊里糊涂的,这是金前辈的总结),缩点完成后先找出每个强连通内的最小花费,最后在根据一开始的有向图给每个强连通连上边,找出入度为零的点,这些点的花费和就是答案
下面是AC代码
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <stack>using namespace std;typedef long long ll;const int maxn = 1007;const ll inf = 0x7fffffffffffffffll;int pre[maxn], low[maxn], sccno[maxn], num;vector<int> edge[maxn];stack<int> s;void init(int n){ /// 初始化 memset( pre, 0, sizeof( pre)); memset( low, 0, sizeof( low)); memset( sccno, 0, sizeof( sccno)); for(int i = 1; i <= n; i ++) edge[i].clear(); num = 0;}void dfs(int key){ /// Tarjan算法 pre[key] = low[key] = ++ num; s.push(key); int tt = edge[key].size(), temp; for(int i = 0; i < tt; i ++){ temp = edge[key][i]; if(!pre[temp]){ dfs(temp); if(low[temp] < low[key]) low[key] = low[temp]; } else if(!sccno[temp]){ if(pre[temp] < low[key]) low[key] = pre[temp]; } } if(low[key] == pre[key]){ /// 缩点 while(s.top() != key){ sccno[s.top()] = key; s.pop(); } sccno[key] = key; s.pop(); }}ll x[maxn], y[maxn], r[maxn], c[maxn];int vis[maxn];bool judge(ll p, ll q, ll w){ /// 范围的判断,此处没有用开方,精度更高 return p*p + q*q <= w*w;}int main(){ int t, n, u = 0; scanf("%d", &t); while(t --){ scanf("%d", &n); for(int i = 1; i <= n; i ++) scanf("%I64d %I64d %I64d %I64d", &x[i], &y[i], &r[i], &c[i]); init(n); for(int i = 1; i < n; i ++) /// 建有向图 for(int j = i + 1; j <= n; j ++){ if(judge(x[i]-x[j], y[i]-y[j], r[i])) edge[i].push_back(j); if(judge(x[i]-x[j], y[i]-y[j], r[j])) edge[j].push_back(i); } for(int i = 1; i <= n; i ++) /// Tarjan 强连通缩点 if(!pre[i]) dfs(i); for(int i = 1; i <= n; i ++) /// 找出每个强连通的最小花费 c[sccno[i]] = min( c[i], c[sccno[i]]); memset( vis, 0, sizeof( vis)); for(int i = 1; i <= n; i ++){ /// 找出每个强连通的入度 int kk = edge[i].size(); for(int j = 0; j < kk; j ++){ int l = edge[i][j]; if(sccno[i] != sccno[l]){ vis[sccno[l]] ++; } } } ll ans = 0; for(int i = 1; i <= n; i ++){ /// 找出入度为零的强连通的花费和 if(sccno[i] == i && !vis[i]) ans += c[i]; } printf("Case #%d: %I64d\n", ++ u, ans); } return 0;}
思路是先对每个点的花费进行排序,然后从花费小的且没被搜到过的点开始DFS,除了一开始搜的点,其他都做上标记,如果搜到的是标记过的点那么返回,最后找出没标记过的点,输出这些点的花费和。
以下是代码(20个点以内找不到错的数据,点多了就错了,I don't know why??)
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int big = 1e3 + 7;vector<int> z[big];struct node{ ll x, y, r, c, num;} a[big];bool vis[big];int record;ll dis(ll kk, ll hh){ return kk*kk + hh*hh;}bool cmp(node u, node v){ return u.c < v.c;}void DFS(int key){ int tt = z[key].size(), ff; for(int i = 0; i < tt; i ++) { ff = z[key][i]; if(!vis[ff] && record != ff) /// record != ff 是防止把开始搜的点给标记上了,因为有可能成环,record 记录了每次开始DFS时的点 { vis[ff] = true; DFS(ff); } }}int main(){ int t, n, u = 0; scanf("%d", &t); while(t --) { scanf("%d", &n); for(int i = 1; i <= n; i ++) { scanf("%I64d %I64d %I64d %I64d", &a[i].x, &a[i].y, &a[i].r, &a[i].c); a[i].num = i; a[i].r *= a[i].r; z[i].clear(); } for(int i = 1; i < n; i ++) /// 建有向图 for(int j = i + 1; j <= n; j ++) { ll temp = dis(a[i].x - a[j].x, a[i].y - a[j].y); if(temp <= a[i].r) z[i].push_back(j); if(temp <= a[j].r) z[j].push_back(i); } memset( vis, false, sizeof( vis)); sort( a + 1, a + n + 1, cmp); for(int i = 1; i <= n; i ++) /// DFS { if(vis[a[i].num]) continue; record = a[i].num; DFS(record); } ll sum = 0; for(int i = 1; i <= n; i ++) if(!vis[a[i].num]) sum += a[i].c; printf("Case #%d: %I64d\n", ++ u, sum); } return 0;}
0 0
- Bomb (hdu 5934 && 2016icpc杭州现场赛题目)
- HDOJ 5934 Bomb【2016杭州现场赛】【强连通】
- 2016ccpc杭州赛 hdu 5934 B.Bomb
- HDU 4455 Substrings 第37届ACM/ICPC 杭州赛区现场赛 C题 (DP)
- hdu 4777 2013ACM/ICPC杭州现场赛 H题 Rabbit Kingdom
- HDU 4778 2013 ACM/ICPC 杭州赛区现场赛 I. Gems Fight!
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5934 && 2016CCPC杭州 B: Bomb(Trajan强连通)
- 2013ACM/ICPC亚洲区杭州站现场赛——题目重现Zhuge Liang's Password
- hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)
- hdu 4472 Count (2012 ACM-ICPC 成都现场赛)
- HDU-4472-Count (2012 ACM/ICPC成都现场赛)
- Ugly Problem(hdu 5920 && 2016ICPC长春现场赛模拟题)
- 2012杭州现场赛
- hdu 4778 Gems Fight!(2013 杭州 现场赛 I 题) 总和一定的博弈,dp
- HDU 4770 Lights Against Dudely -- 2013 杭州赛区现场赛-A(状态压缩)
- HDU 4455 Substrings (2012年杭州赛区现场赛C题)
- 要循序渐进的学,最好现学现用
- Rxjava(过滤类)-Debounce
- Mybatis自定义typehandler
- SQLite3命令
- Day49:Before going to XiaMen
- Bomb (hdu 5934 && 2016icpc杭州现场赛题目)
- 第九周项目2-对称矩阵压缩存储的实现与应用(1)
- 随机采样接口(子函数)
- 获得两个日期相差的月份按照自然月
- 关于编程语言的一点想法
- Codeforces Beta Round #13 D —— Triangles 计算几何
- Linux系统下查找webshell后门的强大命令
- 51nod-【1089 最长回文子串 V2(Manacher算法)】
- hdu1114(完全背包)