并查集 HOJ 2064 Journey to Tibet
2014-02-08 09:53
274 查看
Journey to Tibet
My Tags | (Edit) |
---|
Source : HCPC 2005 FALL | |||
Time limit : 1 sec | Memory limit : 32 M |
100 100 8
0 0 10
0 10 3
10 30 4
1000 1000 4
100 128 3
0
Sample Output[/b]
2 17
题意:选出一个起点,然后从这个起点开始一次只能走30km,问最多能访问多少的寺庙。
思路:其实就是求出一个含有寺庙最多的连通块,并且选取这个连通块中标号最小的点作为起点。这里连通块我们用并查集实现。
代码:#include<iostream>#include<algorithm>#include<vector>#include<math.h>#include<cstdio>#include<string.h>#include<map>#include<cstring>#include<queue>using namespace std;#define eps 1e-6#define mp make_pair#define pb push_backconst int maxn = 1000+10;int n;
vector<int> son[maxn];int p[maxn];int num[maxn];
int find(int x){ if (x==p[x]) return x; return p[x] = find(p[x]);}
void Union(int rt1,int rt2){ int x = find(rt1); int y = find(rt2); if (x==y) return; if (x < y) p[y] = x; else p[x] = y;}
struct Node{ int x , y;}node[maxn];
void init(){ for (int i = 1 ; i <= n ; ++i) { son[i].clear(); p[i] = i; }}
inline int sqr(int x){ return x*x;}
inline int cal_dis(int i,int j){ return sqr(node[i].x-node[j].x)+sqr(node[i].y-node[j].y);}
void input(){ for (int i = 1 ; i <= n ; ++i) { scanf("%d%d%d",&node[i].x,&node[i].y,num+i); } for (int i = 1 ; i < n ; ++i) { for (int j = i+1 ; j <= n ; ++j) { int dis = cal_dis(i,j); if (dis > 900) continue; Union(i,j); } }}
void solve(){ for (int i = 1 ; i <= n ; ++i) { int fa = find(i); son[fa].push_back(i); } int ans = 0 , sum , p; for (int i = 1 ; i <= n ; ++i) { sum = 0; int pp = i; for (int j = 0 ; j < son[i].size() ; ++j) { pp = min(pp,son[i][j]); sum += num[son[i][j]]; } if (sum > ans) { ans = sum; p = pp; } else if (ans==sum && pp < p) p = pp; } printf("%d %d\n",p,ans);}
int main(){ while (scanf("%d",&n),n) { init(); input(); solve(); }}
相关文章推荐
- HOJ2064 - Journey to Tibet - 并查集+暴力
- Hoj 2064 Journey to Tibet(递归)
- HOJ 2064 Journey to Tibet (DFS)
- Hoj 2064 Journey to Tibet
- HOJ Journey to Tibet(小毛病大问题,下标搞混了,导致很严重的后果)
- HackerRank - journey-to-the-moon 【并查集】
- hackerrank Journey to the Moon(并查集)
- alpcOj Journey to the The World's Start
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest J】【二分答案+multiset】Journey to the “The World’
- From journeyman to master
- 《The Pragmatic Programmer: From Journeymen to Master》
- GYM 100801 J.Journey to the “The World’s Start”(二分+dp+线段树)
- Journey to the The World's Start
- hoj2434 going to know him
- ZOJ 3223 Journey to the Center of the Earth
- BZOJ153/POI2005 A Journey to Mars
- Lightoj1009 Back to Underworld(带权并查集)
- Journey from a Python noob to a Kaggler on Python (从Python数据分析菜鸟到专家)
- HOJ 2634 - How to earn more(网络流’最小割)
- Journey to SQLAuthority