zoukankan      html  css  js  c++  java
  • poj 2253 Frogger【最小生成树变形】【kruskal】

    Frogger
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 30427   Accepted: 9806

    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

    题意:给出n个点的坐标,连通第一个点和第二个点有很多条路径,每条路径都会有 一个最大距离d - ->(当前路径经过的点集里面 任意两点间的距离都小于或者等于d)。题目要求找到这样的一个路径:(1)连通1,2两点;(2)这条路径中的最大距离d 是所有可选择路径中最小的。  输出这条路径的最大距离d。

    题解:利用并查集kruskal算法;先将任意两个点之间的距离按从小到大的顺序排列,然后开始枚举所有的边,直至找到使起点和终点联通的边;
    因为要求所有可以连通的路径的最大权值中最小的,所以一旦找到使其连通的边就是所要求的结果
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #define MAX 210
    #define DD double
    using namespace std;
    int set[MAX];
    DD a[MAX],b[MAX];
    int k,t,n;
    struct node
    {
    	int u,v;
    	DD w;
    }s[50000];
    bool cmp(node a,node b)
    {
    	return a.w<b.w;
    }
    DD dis(DD x1,DD y1,DD x2,DD y2)
    {
    	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
    void init()
    {
    	for(int i=0;i<=n;i++)
    	    set[i]=i;
    }
    void getmap()
    {
    	for(int i=0;i<n;i++)
        	scanf("%lf%lf",&a[i],&b[i]);
        k=0;
        for(int i=0;i<n-1;i++)
            for(int j=i+1;j<n;j++)
            {
            	s[k].u=i;
            	s[k].v=j;
            	s[k++].w=dis(a[i],b[i],a[j],b[j]);
    		}
    	sort(s,s+k,cmp);
    }
    int find(int fa)
    {
    	if(fa==set[fa])
    	    return fa;
    	return set[fa]=find(set[fa]);
    }
    void mix(int x,int y)
    {
    	int fx,fy;
    	fx=find(x);
    	fy=find(y);
    	if(fx!=fy)
    	    set[fx]=fy;
    }
    void solve()
    {
    	int i,j;
    	int ok;
    	DD ant;
    	for(i=0;i<k;i++)
    	{
    		ok=0;
    		ant=s[i].w;
    		mix(s[i].u,s[i].v);
    		if(find(0)==find(1))//判断是否连通 
    			ok=1;
    		if(ok)//第一次连通时就是最小的 
    		{
    		    printf("Scenario #%d
    ",++t);
    		    printf("Frog Distance = %.3f
    
    ",ant);
    		    return ;
            }
    	}
    }
    int main()
    {
    	t=0;
    	while(scanf("%d",&n),n)
    	{
    		init();
    		getmap();
    		solve();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    今天一个人跑了趟香山
    周六钻胡同
    (转)C#中protected用法详解
    C# base和this
    error BK1506 : cannot open file '.\Debug\ex73View.sbr': No such file or directory
    error PRJ0003 : Error spawning 'cmd.exe'
    VS2008卸载时遇到“加载安装组件时遇到问题,取消安装” 在卸载或者升级VS2008的时候,遇到“加载安装组件时遇到问题,取消安装”的情况
    我们在建立Win32工程的时候,要选择是Win32控制台应用程序还是Win32项目,那么两者到底有什么区别呢?
    开发板重新烧写时出现ERROR: Checksum failure (expected=0x3D67E6F computed=0x3E0E0CA)
    把PC上的代码移植到WINCE上
  • 原文地址:https://www.cnblogs.com/tonghao/p/4726715.html
Copyright © 2011-2022 走看看