青蛙跳跃,题意大概是:青蛙从起点到终点进行一次或多次的跳跃,多次跳跃中肯定有最大的跳跃距离。求在所有的跳跃中,最小的最大跳跃距离SF-_-(不理解?看题目吧)。

可以用最小生成树完成。以起点为根,生成一棵最小生成树,直到树里包含了终点。

或者这么说吧,类似于Kruskal算法,我们每次选取不成环的最小边,直到这棵树选取了通往终点的最小边,那么最后选择的这条边必然是在树中最大的一条边,而且在其余的边中是最小的。你不会找到比这条边小的最大距离,因为比它小的最小距离都在树里了,而未选取该边前树中不包含终点,即比该边小的所有边无法到达终点。即改边满足的两个条件,最小,而且是起点到终点的最大距离(PS:挺绕的……)。

既然有思路了,可以直接写代码了。先是上面的Kruskal算法(16MS):

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; int root[];
int x[],y[]; int find(int x)
{
return root[x]?root[x]=find(root[x]):x;
} bool union_set(int a,int b)
{
a=find(a);
b=find(b);
if(a==b)
return false;
root[b]=a;
return true;
} struct Edge
{
int x,y,dis;
bool operator<(const Edge& cmp) const
{
return dis<cmp.dis;
}
} edge[]; int main()
{
// freopen("in.txt","r",stdin);
int n,cas=;
while(~scanf("%d",&n) && n)
{
memset(root,,sizeof(root));
for(int i=;i<=n;i++)
scanf("%d%d",x+i,y+i);
int index=;
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
edge[index].x=i;
edge[index].y=j;
edge[index++].dis=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
}
sort(edge,edge+index); int maxE=;
for(int i=;i<index;i++)
{
if(union_set(edge[i].x,edge[i].y))
{
if(find()==find())
{
maxE=edge[i].dis;
break;
}
}
}
printf("Scenario #%d\n",cas++);
printf("Frog Distance = %.3f\n\n",sqrt((double)maxE));
}
}

使用并查集的Kruskal算法明显要好写一点。但是这题每两点之间必然有一条边,是一个稠密图,Prim算法更适合一些。下面是Prim算法的代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std; struct Node
{
int k,w;
bool operator<(const Node& cmp) const
{
return w>cmp.w;
}
} p,q; bool vis[];
int x[],y[];
int first[],vv[],ww[],nxt[]; int main()
{
// freopen("in.txt","r",stdin);
int n,cas=;
while(~scanf("%d",&n) && n)
{
priority_queue <Node> pq;
memset(vis,,sizeof(vis));
memset(first,,sizeof(first)); for(int i=;i<=n;i++)
scanf("%d%d",x+i,y+i); int e=;
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
nxt[e]=first[i],vv[e]=j;
ww[e+]=ww[e]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
first[i]=e++;
nxt[e]=first[j],vv[e]=i;
first[j]=e++;
}
} p.k=;
p.w=;
pq.push(p); int maxE=;
while(!pq.empty())
{
p=pq.top();
pq.pop();
maxE=max(maxE,p.w);
if(p.k==)
break;
if(vis[p.k])
continue;
vis[p.k]=true; for(int e=first[p.k];e;e=nxt[e]) if(!vis[vv[e]])
{
q.k=vv[e];
q.w=ww[e];
pq.push(q);
}
} printf("Scenario #%d\n",cas++);
printf("Frog Distance = %.3f\n\n",sqrt((double)maxE));
}
}

需要注意的是Kruskal算法最终的判定是起点和终点是否同一集合,如果是,最大距离就是最后一条边的距离。而prim算法的最大边需要实时更新,因为先选的边可能大于后来选择的边。搞笑的是Prim算法也是16MS,不知道是不是我写的效率有问题SF0_0。夜深人静的时候在去跑跑吧……

POJ 2253 Frogger(最小生成树)的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2253 Frogger

    题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...

  2. POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)

    POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...

  3. POJ. 2253 Frogger (Dijkstra )

    POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...

  4. POJ 2253 Frogger(dijkstra 最短路

    POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

  5. poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))

    传送门: http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  6. POJ 2253 Frogger

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  7. poj 2253 Frogger (dijkstra最短路)

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  8. POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】

    Frogger Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  9. POJ 2253 Frogger Floyd

    原题链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  10. poj 2253 Frogger (最长路中的最短路)

    链接:poj 2253 题意:给出青蛙A,B和若干石头的坐标,现青蛙A想到青蛙B那,A可通过随意石头到达B, 问从A到B多条路径中的最长边中的最短距离 分析:这题是最短路的变形,曾经求的是路径总长的最 ...

随机推荐

  1. 06_XML的写入_dom4j添加、删除、修改Xml文件内容

    [工程截图] [person.xml]准备一个xml文件 <?xml version="1.0" encoding="UTF-8"?> <st ...

  2. struts2的java文件中不能直接弹出script对话框

    需要引入接口 ServletResponseAware public class Login extends ActionSupport implements SessionAware,Servlet ...

  3. OpenJudge / Poj 1928 The Peanuts C++

    链接地址:http://bailian.openjudge.cn/practice/1928 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 Mr. Robinson and h ...

  4. 在ctex环境下利用Metapost作图

    使用Metapost作图,是LaTeX的好搭档.下面介绍如何在ctex环境下的使用Metapost作图. 首先新建一个test.mp的Metapost文件. 在文件开始需要声明如下代码: prolog ...

  5. rsync+inotity

    rsync默认端口:873xinetd默认服务 inotify参数详解inotifywait-r:递归-q:只打印事件-m:始终监听事件--excludei:排除--timefmt:时间格式--for ...

  6. LumiSoft收取邮件(含邮件附件)

    在.NET当中利用C#发送电子邮件很简单,微软也提供了默认的实现,但是收取电子邮件的操作却并没有提供解决方案.好在有一些第三方的解决方案可供选择,来简化程序员日常项目中的开发工作. 这里我选用Lumi ...

  7. Visual C++ 对话框增加菜单栏

    1.添加菜单资源     在resourceview视图中右击选择insert,添加一个菜单资源IDR_MENU1,同时设定好响应的菜单项,例 如:         菜单1               ...

  8. SQL学习_时间函数

    最近测试报表需要统计不同时间段的列表记录,收集一些时间函数作为参考,原文地址:http://blog.csdn.net/lyzlyfok/article/details/6282509 sql ser ...

  9. LocalContainerEntityManagerFactoryBean

    http://doc.okbase.net/liuyitian/archive/109276.html

  10. Linux安装包

    关于SWT SWT首先要在Eclipse中添加SWT的安装包:Windowsbuilder Pro.下载路径:http://www.eclipse.org/windowbuilder/download ...