zoukankan      html  css  js  c++  java
  • (最短路 Floyd diskstra prim)Frogger --POJ--2253

    题目链接:http://poj.org/problem?id=2253

    Frogger
    
    
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 31114   Accepted: 10027
    
    

    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



    题意:
    从起点到终点会有很多路径,每条路径上的边有一个最大值,求这些最大值中的最小值。
    也就是更新的边要保持最大边。

    Floyd
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 using namespace std;
    10 
    11 
    12 #define INF 0x3f3f3f3f
    13 #define N 300
    14 struct node
    15 {
    16     int x, y;
    17 };
    18 
    19 double dist[N];
    20 double G[N][N];
    21 int vis[N], n;
    22 
    23 void IN()
    24 {
    25     memset(vis, 0, sizeof(vis));
    26 
    27     for(int i=1; i<=n; i++)
    28     {
    29         dist[i]=INF;
    30         for(int j=1; j<=i; j++)
    31             G[i][j]=G[j][i]=INF;
    32     }
    33 }
    34 
    35 void Floyd()
    36 {
    37     for(int k=1; k<=n; k++)
    38     {
    39         for(int j=1; j<=n; j++)
    40         {
    41             for(int i=1; i<=n; i++)
    42             {
    43                 if(G[j][i] > max(G[j][k], G[k][i]))
    44                 G[j][i] = max(G[j][k], G[k][i]);
    45             }
    46         }
    47     }
    48 }
    49 
    50 int main()
    51 {
    52    int i, j, t=1;
    53 
    54    while(scanf("%d", &n), n)
    55    {
    56        double  w;
    57        node s[N];
    58        memset(s, 0, sizeof(s));
    59        IN();
    60 
    61        for(i=1; i<=n; i++)
    62            scanf("%d%d", &s[i].x, &s[i].y);
    63 
    64        for(i=1; i<n; i++)
    65        for(j=i+1; j<=n; j++)
    66        {
    67            w = sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)*1.0 + (s[i].y-s[j].y)*(s[i].y-s[j].y)*1.0);
    68            G[i][j] = G[j][i]=min(G[i][j], w);
    69        }
    70 
    71        printf("Scenario #%d
    ", t++);
    72 
    73        Floyd();
    74 
    75        printf("Frog Distance = %.3f
    
    ", G[1][2]);
    76 
    77    }
    78     return 0;
    79 }

     dijkstra

     #include<iostream>
     #include<cstdio>
     #include<cstring>
     #include<cstdlib>
     #include<cmath>
     #include<algorithm>
     #include<queue>
    
     using namespace std;
    
    
     #define INF 0xfffffff
     #define N 1100
     struct node
     {
         int x, y;
     }a[N];
    
     int n, vis[N];
    double dist[N], G[N][N];
    
    void Dij()
    {
        int i, j;
    
        for(i=1; i<=n; i++)
        {
            dist[i] = G[1][i];
            vis[i] = 0;
        }
        vis[1] = 1;
    
        for(i=1; i<n; i++)
        {
            int index=1;
            double Min=INF;
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]<Min)
                {
                   Min = dist[j];
                   index = j;
                }
            }
    
            if(index==1)
                continue;
    
            vis[index] = 1;
    
            for(j=1; j<=n; j++)
                if(!vis[j] && max(dist[index], G[index][j])<dist[j])
                dist[j] = max(dist[index], G[index][j]);
        }
    }
    
    int main()
    {
        int iCase = 1;
    
        while(scanf("%d", &n), n)
        {
            int i, j;
    
            for(i=1; i<=n; i++)
                scanf("%d%d", &a[i].x, &a[i].y);
    
            for(i=1; i<=n; i++)
            for(j=1; j<=i; j++)
            {
                double d = sqrt( (a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y) );
                G[i][j] = G[j][i] = d;
            }
    
            Dij();
            printf("Scenario #%d
    ", iCase++);
            printf("Frog Distance = %.3f
    
    ", dist[2]);
        }
        return 0;
    }

     prim

    类似于最小生成树

    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    using namespace std;
    const int INF = (1<<30)-1;
    #define min(a,b) (a<b?a:b)
    #define max(a,b) (a>b?a:b)
    #define N 1100
    
    struct node
    {
        int x, y;
    }a[N];
    
    int n, m;
    double dist[N], G[N][N];
    int vis[N];
    
    double prim()
    {
        int i, j;
        double ans = -1;
    
        for(i=1; i<=n; i++)
            dist[i] = G[1][i];
        dist[1] = 0;
    
        memset(vis, 0, sizeof(vis));
        vis[1] = 1;
    
        for(i=1; i<=n; i++)
        {
            int index = 1;
            double Min = INF;
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]<=Min)
                {
                    Min = dist[j];
                    index = j;
                }
            }
    
            if(index==1) break;
    
            vis[index] = 1;
    
            ans = max(ans, Min);
    
            if(index==2) return ans;
    
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]>G[index][j])
                   dist[j] = G[index][j];
            }
        }
    
        return ans;
    }
    
    
    int main()
    {
        int  iCase=1;
        while(scanf("%d", &n), n)
        {
            int i, j;
    
            memset(a, 0, sizeof(a));
    
            for(i=1; i<=n; i++)
            scanf("%d%d", &a[i].x, &a[i].y);
    
            for(i=1; i<=n; i++)
            for(j=1; j<=i; j++)
               G[i][j] = G[j][i] = sqrt( (a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y) );
    
            printf("Scenario #%d
    ", iCase++);
            printf("Frog Distance = %.3f
    
    ", prim());
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    九度oj 题目1465:最简真分数
    九度oj 题目1083:特殊乘法 清华大学2010年机试题目
    九度oj 题目1084:整数拆分 清华大学2010年机试题目
    九度oj 题目1085:求root(N, k) 清华2010年机试题目
    九度oj 题目1460:Oil Deposit
    九度oj 题目1459:Prime ring problem
    九度oj 题目1458:汉诺塔III
    九度oj 题目1457:非常可乐
    题目1451:不容易系列之一
    移动端滚动不流畅,添加-webkit-overflow-scrolling属性 值为touch
  • 原文地址:https://www.cnblogs.com/YY56/p/4658201.html
Copyright © 2011-2022 走看看