zoukankan      html  css  js  c++  java
  • 【最短路入门专题1】H

    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,输入顶点1和顶点n的坐标,再输入2~n-1的顶点坐标,输出最大边权的最小值。

    思路:dijktra搞定的,松弛条件要改一下,对于同一条路径,原模板dis存的是最短路之和,现在我们改为存一条路径上的最大边权,所以每次比较的时候,1:取得值是当前路径的边权和目前的最大边权比较后较大那个值,2:只有一条路的最大边权比另一条路的最大边权大时,我们才取小的值存入。具体实现步骤见代码

    总结:英语太渣。还有以后交语言不要交G++,直接交c++,上台敲代码wrong到爆炸,后来才发现是交错了语言

    #include<stdio.h>  
    #include<string.h>  
    #include<math.h>  
    #include<stdlib.h>  
    #define inf 99999999  
    int book[220];  
    double dis[220],x,max;  
    struct node{  
        double x;  
        double y;  
    };  
      
    node edge[220];  
    double Max(double a,double b)  
    {  
        if( a > b)  
            return a;  
        return b;  
    }  
    int main()  
    {  
        int n;  
        int i,j,u,t2,count;  
        t2 = 0;  
        while(scanf("%d",&n),n!=0)  
        {  
            count = 0;  
            t2++;  
            memset(book,0,sizeof(book));  
            memset(dis,0,sizeof(dis));  
            scanf("%lf%lf",&edge[1].x ,&edge[1].y);  
            scanf("%lf%lf",&edge[n].x ,&edge[n].y );  
            for(i = 2; i < n; i ++)  
                scanf("%lf%lf",&edge[i].x ,&edge[i].y );  
            for(i = 2; i <= n; i ++)  
            {  
                x =  sqrt(pow(fabs(edge[i].x-edge[1].x),2)+pow(fabs(edge[i].y-edge[1].y),2));  
                dis[i] = x;  
            }  
          
            book[1] = 1;      
            while(count < n)  
            {  
                max = inf;  
                for(j = 1; j <= n; j ++)  
                {  
                    if(!book[j]&&dis[j] < max)  
                    {  
                        max = dis[j];  
                        u = j;  
                    }  
                }  
                book[u] = 1;
                count ++;  
                for(j = 1; j <= n; j ++)  
                {  
                    x = Max(dis[u],sqrt(pow(fabs(edge[j].x-edge[u].x),2)+pow(fabs(edge[j].y-edge[u].y ),2)));  //  1
      
                    if(!book[j]&&dis[j] > x)  
                    {  
                        dis[j] = x;  //  2
                    }  
                          
                }  
            }  
            printf("Scenario #%d
    ",t2);  
            printf("Frog Distance = %.3lf
    
    ",dis[n]);  
        }  
        return 0;  


  • 相关阅读:
    linux取随机数shell版本
    PWD简介与妙用(一个免费、随时可用的Docker实验室)
    MySQL基础教程——mysql脚本编写
    MySQL基础教程——创建数据库并插入数据
    MySQL——数据库和 SQL 概念&&MySQL的安装
    卷积计算
    卷积及其应用
    SG函数入门&&HDU 1848
    ACM博弈论基础
    Aho-Corasick自动机
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350071.html
Copyright © 2011-2022 走看看