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多条路径中的最长边中的最短距离 分析:这题是最短路的变形,曾经求的是路径总长的最 ...
随机推荐
- 2016.7.2this的应用
this有三个应用: 1.就是在类的方法中参数与成员参数重名了,那么用this.参数名=参数名来区分它们: 2.当一个引用对象要调用另一个已经有具体实例的引用对象,那么通过在类的定义中后面加publi ...
- 小shell函数
whoport() { port=$1 echo "------ who occupied port: $port ----------" info=$(sudo lsof ...
- 用EPPlus导入导出数据到excel
项目上中要用到将数据库中所有表导出为Excel,以及将Excel数据导入数据库中的操作,使用EPPlus组件,编写以下两个函数. using OfficeOpenXml;using OfficeOpe ...
- cms开发笔记2
1 创建数据库表 //配置文件CREATE TABLE IF NOT EXISTS `mc_config` ( `en_name` varchar(80) NOT NULL, `ch_name` va ...
- Python爬虫(小练习)
近日,在浏览伯乐在线(http://blog.jobbole.com/29281/)的时候碰到一些很不错的资源:25本免费的Python电子书 如下图: 其中,每本都是以名字+超链接的方式,于是激起了 ...
- Smlusm随笔目录索引
1.MVVM Light Toolkit ) [转]Mvvm Light Toolkit for wpf/silverlight系列之搭建mvvmlight开发框架 2) [转]Mvvm Light ...
- JLink软件升级到4.92之后,Jlink不能用了
JLink软件升级到4.92之后,Jlink不能用了 情景描述: Jlink软件升级到4.9 ...
- 把Mvc4项目部署到虚拟目录之后找不到control想到的文件路径规范的问题
最近部署的项目的时候由于端口不够用,想到了把Mvc项目部署到虚拟目录中,结果发现图片,js设置control都找不到了.项目是mvc4+easyui开发的,大量的代码都是在js中调用control,写 ...
- CoreBluetooth - 中心模式
BLE中心模式流程-coding BLE中心模式流程 - 1.建立中心角色 - 2.扫描外设(Discover Peripheral) - 3.连接外设(Connect Peripheral) - 4 ...
- .net 访问远程的MSSQL报System.AccessViolationException错误的解决方法
访问远程的数据库时 报错,本地数据库正常 netsh winsock reset --运行此命令,解决. netsh winsock reset命令,作用是重置 Winsock 目录.如果一台机器 ...