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;
    }
    

      

  • 相关阅读:
    HDU 4990 Reading comprehension(BestCoder Round #8)
    HDU 4985 Little Pony and Permutation(BestCoder Round #7)
    HDU 4983 Goffi and GCD(欧拉函数模板)
    CodeForces 590A Median Smoothing
    CodeForces 591B Rebranding
    LightOJ 1100
    Load average 负载详细解释
    Loadrunner 上传下载
    使用VisualVM远程监控JVM Linux服务器配置方法
    JVM调优(7)调优方法
  • 原文地址:https://www.cnblogs.com/tonghao/p/4726715.html
Copyright © 2011-2022 走看看