poj2253 最小生成树中的最大边 prim

Frogger

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30620   Accepted: 9875

Description

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists‘ sunscreen, he wants to avoid swimming
and instead reach her by jumping.

Unfortunately Fiona‘s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.

To execute a given sequence of jumps, a frog‘s jump range obviously must be at least as long as the longest jump occuring in the sequence.

The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy‘s stone, Fiona‘s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy‘s and Fiona‘s stone.

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates
of stone #i. Stone #1 is Freddy‘s stone, stone #2 is Fiona‘s stone, the other n-2 stones are unoccupied. There‘s a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three
decimals. Put a blank line after each test case, even after the last one.

Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0

Sample Output

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

Source

//题意要去将所有的石头都连起来,求最大边

Ulm Local 1997

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 100000000.0

using namespace std;

struct node
{
    int x,y;
} point[300];

double chuli(node p1,node p2)
{
    return pow((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y),1.0/2);
}
double Map[1200][1200];
double a[2000];
double low[2000];
int next[2000];
int n;
int Prim(int u0)
{
    int i,j;
    for(i=0; i<n; i++)
    {
        low[i]=Map[u0][i];
        next[i]=u0;
    }
    next[u0]=-1;
    double max=0.0;
    for(i=0; i<n-1; i++)
    {

        double min=INF;
        int v=-1;
        for(j=0; j<n; j++)
        {
            if(next[j]!=-1&&low[j]<min)
            {
                min=low[j];
                v=j;
            }
        }

        if(min>max)
            max=min;      //本体的核心
        if(a[v]<0.0000001)             //
        {
            a[v]=max;
            if(v==1)
                break;
        }

        if(v!=-1)
        {
            next[v]=-1;
            for(j=0; j<n; j++)
            {
                if(next[j]!=-1 &&Map[v][j]<low[j])
                {
                    low[j]=Map[v][j];
                    next[j]=v;
                }
            }
        }
    }
    printf("Frog Distance = %.3f\n",a[1]);  //poj不支持 %lf
}

int main()
{
	int Case=1;
    while(~scanf("%d",&n))
    {
    	if(n==0)
			break;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        for(int i=0; i<n; i++)
            a[i]=0.0;
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
            {
                Map[i][j]=chuli(point[i],point[j]);
            }
		printf("Scenario #%d\n",Case++);
        Prim(0);
        printf("\n");
        //cout<<"*";
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 17:09:10

poj2253 最小生成树中的最大边 prim的相关文章

最小生成树之Kruskal算法和Prim算法

依据图的深度优先遍历和广度优先遍历,能够用最少的边连接全部的顶点,并且不会形成回路. 这样的连接全部顶点并且路径唯一的树型结构称为生成树或扩展树.实际中.希望产生的生成树的全部边的权值和最小,称之为最小生成树. 常见的最小生成树算法有Kruskal算法和Prim算法. Kruskal算法每次选取权值最小的边.然后检查是否增加后形成回路,假设形成回路则须要放弃.终于构成最小生成树.n个顶点的图最小生成树过程例如以下: 边的权值升序排序. 选取全部未遍历的边中权值最小的边,推断增加后是否形成回路,若

hihoCoder #1109 最小生成树之堆优化的Prim算法

原题地址:http://hihocoder.com/problemset/problem/1109 #1109 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生了一个疑问,究竟这样的算法在稀疏图上比Prim优化之处在哪里呢? 提示:没有无缘无故的优化! 输入 每个测试点(输入文件)有且仅有一组测试数据. 在一组测试数据中: 第1行为2个整数N.M,表示小

图的最小生成树之普里姆Prim算法

源代码如下: #include<iostream> using namespace std; #define MAX_VERTEX_NUM 20 #define infinity 9 typedef int QElemType; typedef int EdgeData; typedef char VertexData; typedef struct { VertexData verlist[MAX_VERTEX_NUM]; //顶点表 EdgeData edge[MAX_VERTEX_NUM

hiho一下 第二十九周 最小生成树三&#183;堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】

题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生了一个疑问,究竟这样的算法在稀疏图上比Prim优化之处在哪里呢? 提示:没有无缘无故的优化! 输入 每个测试点(输入文件)有且仅有一组测试数据. 在一组测试数据中: 第1行为2个整数N.M,表示小Hi拥有的城市数量和小Hi筛选出路线的条数. 接下来的M行,每行描述一条路线,其中第i行为3个整数N1_

POJ 2485 Highways (求最小生成树中最大的边)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i

转 最小生成树(kruskal 算法 和prim算法)

链接:http://blog.csdn.net/weinierbian/article/details/8059129/ 给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树. 求最小生成树的算法(1) 克鲁斯卡尔算法图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.(2) 普里姆算法图的存贮结构采用邻接矩阵.此方法是按各个顶点连通的步骤进行,需要用一个顶点集合,开始为空集,以后将以连通的顶点

最小生成树算法详解(prim+kruskal)

最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里姆)算法求出.最小生成树其实是最小权重生成树的简称. prim: 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小. p

图的最小生成树(普利姆prim算法)

什么是生成树呢? 一个连通图的生成树是指一个极小连通子图, 它含有图中的全部顶点,但只有足以构成一棵树的n-1条边. 什么是最小生成树? 在一个连通图的所有生成树中,各边的代价之和最小的那棵生成树称为该连通图的最小代价生成树(MST), 简称最小生成树. 求最小生成树有两种算法,本文讲prim算法. 简略证明 使用反证法证明 设一棵最小生成树T不包含最短边a,将a加入最小生成树T中,书中必定构成一个包含a的回路,而回路中必定有边比a大(因a为最短边),则删除比a大的边得到一棵比原先T更小的树T1

POJ-2349(kruskal算法+最小生成树中最大边的长度)

Arctic POJ-2349 这题是最小生成树的变形题目.题目的意思是已经有s个卫星频道,这几个卫星频道可以构成一部分的网络,而且不用费用,剩下的需要靠d的卫星接收器.题目要求的就是最小生成树中,最大的边的长度. 题目中的传入kruskal函数里面的sn表示还需要连接的顶点个数,因为剩下的可以使用卫星频道来连接. 剩下的就是kruskal的问题了,这里通过当前最大边来返回答案. #include<iostream> #include<cstdio> #include<alg