WOJ1038-Help!

来源:互联网 发布:python cdll 编辑:程序博客网 时间:2024/06/10 20:29

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 10 01 12 20 01 10

样例输出

Poor magicpig!Success!


//最小生成树 #include<iostream>#include<cstdio>#include<cmath> #include<algorithm>using namespace std;const int maxn=105,maxm=5000;int x[maxn],y[maxn];//存储节点信息int u[maxm],v[maxm];//边的两个端点int r[maxm];//边的序号 float w[maxm];//边的长度int p[maxn];//并查集int n;int cmp(const int i,const int j){return w[i]<w[j];}//间接排序函数 int find(int a){return p[a]==a?a:p[a]=find(p[a]);}//并查集的find函数 int main(){int i,j,m=-1,L;float ans;while(scanf("%d",&n)&&n!=0){scanf("%d",&L);ans=0;m=-1;for(i=0;i<n;i++){scanf("%d %d",&x[i],&y[i]);}for(i=0;i<n;i++)//计算两两节点之间的距离 for(j=i+1;j<n;j++){m++;u[m]=i;v[m]=j;w[m]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}for(i=0;i<n;i++)p[i]=i;for(i=0;i<=m;i++)r[i]=i;sort(r,r+m+1,cmp);for(i=0;i<=m;i++){int e=r[i];int x=find(u[e]);int y=find(v[e]);//找出当前边两个端点的所在集合编号 if(x!=y){ans+=w[e];p[x]=y;//如果在不同集合,合并 } }if(ans<=L)printf("Success!\n");elseprintf("Poor magicpig!\n");}return 0;} 


原创粉丝点击