poj 2253 -- Frogger (并查集+贪心)
来源:互联网 发布:c语言音乐点播制作 编辑:程序博客网 时间:2024/06/16 06:21
给出石头的坐标,求从起始到终点路途中每步最短距离(注意不是总距离最短)
只需枚举每两个石头间距离,排序,从小到大依此加入图中,直到起始和终点在同一个集合中即可,证明略
#include<iostream>#include<cstdlib>#include<cmath>#include<cstdio>using namespace std;struct Coor{ int x,y;} coor[201];struct Edge{ int u,v,w;} edge[50000];int pre[201];int find(int x){ if(pre[x]!=x) pre[x]=find(pre[x]); return pre[x];}void Union(int x,int y) { pre[find(x)]=find(y);}int cmp(const void* p1,const void* p2) { return ((Edge*)p1)->w-((Edge* )p2)->w;}int main(){ int cnt,scena=0; while(cin>>cnt,cnt) { for(int i=0;i<201;pre[i++]=i); for(int i=1;i<=cnt;cin>>coor[i].x>>coor[i].y,i++); int num=0; for(int i=1;i<=cnt;i++) for(int j=i+1;j<=cnt;j++) { edge[num].u=i; edge[num].v=j; edge[num].w=(coor[i].x-coor[j].x)*(coor[i].x-coor[j].x)+(coor[i].y-coor[j].y)*(coor[i].y-coor[j].y); num++; } qsort(edge,num,sizeof(Edge),cmp); int i=0; for(i=0;i<num;i++) { Union(edge[i].u,edge[i].v); if(find(1)==find(2)) break; } double ans=sqrt(edge[i].w); printf("Scenario #%d\nFrog Distance = %.3f\n\n",++scena,ans); }}
- poj 2253 -- Frogger (并查集+贪心)
- POJ 2253 Frogger(并查集+二分)
- poj 2253 Frogger 【枚举+并查集 or 最短路】
- POJ 2377 Bad Cowtractors(并查集+贪心)
- POJ 1456 Supermarket(贪心 + 并查集 )
- POJ-1456 Supermarket(贪心,并查集优化)
- POJ 1456 supermarket(贪心加并查集)
- poj 1456 Supermarket (贪心, 并查集)
- POJ 1456 Supermarket【贪心 + 并查集】
- poj 1456 贪心+并查集优化
- poj 1456 - Supermarket(贪心+并查集)
- poj 1456 Supermarket (贪心+并查集)
- POJ 1456 Supermarket【贪心+并查集】
- POJ 1456 Supermarket [贪心+并查集]
- Supermarket poj 1456 (贪心,并查集)
- POJ-1456 Supermarket【贪心+并查集】
- 【经典贪心】poj 1456 Supermarket (贪心, 并查集)
- poj 1456 Supermarket(贪心+并查集,贪心+优先队列)
- Windows XP安装MTP驱动
- 用boost实现的一个简易线程池库
- Spring整合Quartz定时发送邮件
- 回溯数独
- 解读IE10及IE10+HTML5开发新机遇
- poj 2253 -- Frogger (并查集+贪心)
- 寒深
- ORACLE 误操作方案
- uva11645
- php 显示sqlserver二进制图片
- 全球定位平台
- qt--windows下安装
- <转载> Timer和ScheduledThreadPoolExecutor的区别
- 2013年的13个设计趋势