zoukankan      html  css  js  c++  java
  • Frogger(最短路)

    Frogger(poj2253)
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 31854   Accepted: 10262

    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

    大意:
    从A到B有多条路径,先取每条路径的最大值,然后再求出它们的最小值。
    比如三条路径,1 3 4;2 5,1 2 1;最大值分别为4,5,2,最小值就是2

    使用DIjkstra算法的思路,lowcost[]此时不再是最短路径,而是从起点到结点i的所有路径的最大边中的最小值,算法中维护s集合,从未更新的结点中取出最小节点,不断向外进行扩展。
    扩展方法:if(max(lowcost[u],w[u][v])<lowcost[v])
          lowcost[v]=
    max(lowcost[u],w[u][v])<lowcost[v];
    
    
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <cstring>
     5 using namespace std;
     6 #define Max 300+10
     7 #define MMax 0x3f3f3f3f
     8 struct point
     9 {
    10     int x,y;
    11 };
    12 double w[Max][Max];
    13 double lowcost[Max];
    14 bool vis[Max];
    15 point p[Max];
    16 int n;
    17 double dis(point a,point b)
    18 {
    19     return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    20 }
    21 void Dijkstra(int s)
    22 {
    23     for(int i=0;i<n;i++){
    24         lowcost[i]=MMax;        /*memset按照字节来赋值,但是int是四个字节*/
    25         vis[i]=0;
    26     }
    27     lowcost[0]=0;
    28     while(1)
    29     {
    30         int u=-1,v;
    31         int i,j;
    32         double Min=MMax;
    33         for(v=0;v<n;v++)
    34             if(!vis[v]&&(u==-1||lowcost[v]<lowcost[u]))
    35             {
    36                 u=v;
    37             }
    38         if(u==-1)    break;
    39         vis[u]=1;
    40         for(v=0;v<n;v++)
    41             if(!vis[v]&&max(lowcost[u],w[u][v])<lowcost[v])
    42                 lowcost[v]=max(lowcost[u],w[u][v]);
    43     }
    44     return;
    45 }
    46 int main()
    47 {
    48     int i,j,k,count=0;
    49     //freopen("in.txt","r",stdin);
    50     while(cin>>n&&n)
    51     {
    52         memset(w,0,sizeof(0));
    53         for(i=0;i<n;i++)
    54             scanf("%d%d",&p[i].x,&p[i].y);
    55         for(i=0;i<n-1;i++)
    56             for(j=i+1;j<n;j++)
    57                 w[i][j]=w[j][i]=dis(p[i],p[j]);
    58         Dijkstra(0);
    59         printf("Scenario #%d
    Frog Distance = %0.3lf
    
    ",++count,lowcost[1]);
    60     }
    61 }
     
  • 相关阅读:
    UVALive 4764 简单dp水题(也可以暴力求解)
    poj 2151 概率DP(水)
    poj 2299 归并排序求逆序数 (可做模板)
    poj2388 更水
    poj1936 假期计划第一水
    poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
    UVA315:Network(求割点)
    POJ1236:Network of Schools (思维+Tarjan缩点)
    SPOJ
    HDU4305:Lightning(生成树计数+判断点是否在线段上)
  • 原文地址:https://www.cnblogs.com/a1225234/p/4993639.html
Copyright © 2011-2022 走看看