POJ 2253 Frogger(最小生成树)
青蛙跳跃,题意大概是:青蛙从起点到终点进行一次或多次的跳跃,多次跳跃中肯定有最大的跳跃距离。求在所有的跳跃中,最小的最大跳跃距离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(最小生成树)的更多相关文章
- 最短路(Floyd_Warshall) POJ 2253 Frogger
题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...
- POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)
POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))
传送门: http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 2253 Frogger
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】
Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- POJ 2253 Frogger Floyd
原题链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2253 Frogger (最长路中的最短路)
链接:poj 2253 题意:给出青蛙A,B和若干石头的坐标,现青蛙A想到青蛙B那,A可通过随意石头到达B, 问从A到B多条路径中的最长边中的最短距离 分析:这题是最短路的变形,曾经求的是路径总长的最 ...
随机推荐
- WPF FileFolderDialog 和弹出子窗口的一些问题
摘要:本文主要是WPF中 FileFolderDialog的相关问题,补充了关于在父窗口弹出子窗口,以及子窗口的相关属性(Data Binding)和命令绑定(Delegate Command)问题, ...
- 浅析JAVA设计模式(一)
第一写技术博客,只是想把自己一天天积累的东西与大家分享.今天在看<大型网站架构和java中间件>这本书时,其中提到代理模式的动态代理.作为java中间件的一个重要基础,我觉的有必要整理和分 ...
- svn 提交 commit慢
又修改了一下,上一个方法有问题 #!/bin/bash ###ubuntu下注意要用bash哦,不然for循环总提示'bad loop....' export LC_CTYPE=en_US.UT ...
- Linux C 程序 输入输出函数(THREE)
标准输入输出函数#include<stdio.h>stdio 是 standard input & output 的缩写 字符数据输入输出函数: putchar() , getch ...
- Sublime Text 3插件安装方法
安装Sublime Tex 3t插件的方法: 按快捷键Ctrl + ~ 调出console 粘贴以下代码到console并回车: import urllib.request,os; pf = 'Pac ...
- 请求与通配符 mime 映射相匹配。请求映射到静态文件处理程序。如果有不同的前提条件,请求将映射到另一个处理程序。
打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Framework\v4. ...
- 扩展:gridview 空数据时显示表头
2015年7月14日16:50:06 Gridview 默认展示数据时,若数据为空,则表格不显示,显示不美观. 针对此问题进行扩展: using System.Web.UI.WebControls; ...
- 常用PHP缓存技术
1.全页面静态化缓存 也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解析的流程. 一种比较常用的实现方式是用输出缓存: Ob_start() ******要 ...
- 【pyhton】【转】修改递归次数
import sys sys.setrecursionlimit(1500) # set the maximum depth as 1500 def recursion(n): if(n <= ...
- Pentaho Data Integration笔记 (四):Kitchen
官方网站: http://wiki.pentaho.com/display/EAI/Kitchen+User+Documentation Kitchen Kitchen是一个可以执行Spoon编辑的J ...