WHU-ACM Help!
时间限制: 1000 ms
内存限制: 524288 KiB
输入 / 输出: stdio
难度: 3 / 5
题目
Help! Poor Magicpig is in trouble!
There is a large room in the Pyramid called Room-of-No-Return. Very unfortunately, Magicpig is trapped in this room now! There are some hooks on the floor of the room. On the wall of the room there are some ancient Egyptian characters: “If you want to escape from here, you must connect the hooks with ropes such that any two hooks are linked to each other directly or indirectly, then a secret door will open and you will be free. If you can’t do that, you will be trapped here for ever! hiahia…” Fortunately, Magicpig has a rope of length L.
He can cut it into pieces, each piece can connect two hooks if and only if their distance is less or equal to the length of this piece of rope.
If the rope is not long enough,he can’t escape!
Kinfkong wants to know whether he could escape. If he can’t escape, Kinfkong will go to rescue him.
输入格式
The input contains one or more data sets. At first line of each input data set there is two integers n and L, where n is the number of hooks and L is the length of the rope. The next n lines contain series of coordinates of the hooks. Each coordinate is a nonnegative integer pair,and each integer is less than 32768. Each pair is separated by blank. (2<=n<=100, L<=32767)
Zero at line for number of hooks terminates the input for your program.
输出格式
For each data set, if Magicpig can escape, print a string "Success!", else print "Poor magicpig!".
样例输入
2 1
0 0
1 1
2 2
0 0
1 1
0
样例输出
Poor magicpig!
Success!
代码
#include <cstdio>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
const int maxv = 110;
const int maxe = 10010;
int fa[maxv]; //并查集
struct node{
int u, v;
double c;
}a[maxe]; //保存路径
struct point{
double x,y;
}Point[maxv]; //保存点
bool cmp(node b, node d){
return b.c < d.c;
}
int find(int t){
return fa[t]==t ? t:fa[t]=find(fa[t]);
}
double dis(point b, point d){
double res;
res = sqrt((b.x - d.x) * (b.x - d.x) + (b.y - d.y) * (b.y - d.y));
return res;
}
int main(){
int n, m;
int L;
double ans;
char s[] = "Success!";
char d[] = "Poor magicpig!";
while(~scanf("%d",&n)&&n){
ans = 0.0;
m = 0;
scanf("%d",&L);
for(int i = 1; i <= n; i++){
scanf("%lf%lf", &Point[i].x, &Point[i].y);
}
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
a[m].u = i;
a[m].v = j;
a[m++].c = dis(Point[i], Point[j]);
}
}
sort(a, a + m, cmp);
for(int i = 1; i <= n; i++){
fa[i] = i;
}
for(int i = 0; i < m; i++){
int p = find(a[i].u), q = find(a[i].v);
if(p != q){
fa[p] = q;
ans += a[i].c;
}
}
if(ans <= L){
cout<<s<<endl;
} else {
cout<<d<<endl;
}
}
return 0;
}