zoukankan      html  css  js  c++  java
  • POJ 2253 Frogger【最短路变形/最小生成树的最大权/最小瓶颈树/A到B多条路径中的最小的最长边】

    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】

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define mp make_pair
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<=(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 50000+20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    double dp[500][500];
    int t,n;
    int x[500],y[500];
    void floyd()
    {
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    dp[i][j] = min(dp[i][j], max(dp[i][k],dp[j][k]));
                }
            }
        }
    }
    int main()
    {
        int ca=1;
        while(~scanf("%d",&n),n)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d%d",&x[i],&y[i]);
            }
            rep(i,1,n) rep(j,i+1,n)
                dp[i][j]=dp[j][i]=sqrt(double(sqr(x[i]-x[j]))+double(sqr(y[i]-y[j])));
            floyd();
            printf("Scenario #%d
    Frog Distance = %.3f
    
    ",ca++,dp[1][2]);
        }
    }
    /*
    【题意】
    有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,  另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4
    
    【类型】
    最短路/最大边最小的最小瓶颈生成树
    
    【分析】
    最短路松弛的时候条件变一下就可以了。
    
    【时间复杂度&&优化】
    
    【trick】
    
    【数据】
    
    */
    A到B多条路径中的最长边中的最短距离

     【Dij矩阵版】

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define mp make_pair
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<=(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 50000+20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    double dp[500][500],dis[maxn];
    int t,n;
    int x[500],y[500],vis[maxn];
    /*
    2
    0 0
    3 4
    
    3
    17 4
    19 4
    18 5
    
    0
    
    */
    void dij(int s)
    {
        ms(vis,0);
        rep(i,1,n) dis[i]=INF;
        dis[s]=0;
    
        rep(i,1,n)
        {
            int Min=INF,k;
            rep(j,1,n)
                if(!vis[j] && dis[j]<Min)
                    Min=dis[k=j];
            vis[k]=1;
            rep(j,1,n)
                dis[j]=min(dis[j],max(dis[k],dp[k][j]));
        }
    
    }
    int main()
    {
        int ca=1;
        while(~scanf("%d",&n),n)
        {
            ms(dp,0);
            for(int i=1;i<=n;i++)
                scanf("%d%d",&x[i],&y[i]);
            rep(i,1,n) rep(j,i+1,n)
                dp[i][j]=dp[j][i]=sqrt(double(sqr(x[i]-x[j]))+double(sqr(y[i]-y[j])));
            dij(1);
            printf("Scenario #%d
    Frog Distance = %.3f
    
    ",ca++,dis[2]);
        }
    }
    /*
    【题意】
    有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,  另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4
    
    【类型】
    最短路/最大边最小的最小瓶颈生成树
    
    【分析】
    最短路松弛的时候条件变一下就可以了。
    
    【时间复杂度&&优化】
    
    【trick】
    
    【数据】
    
    */
    dij算法

    【kruskal】

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define mp make_pair
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<=(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 1e5+20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    double x[maxn],y[maxn];
    double dis[maxn];
    int t,n,m,u,v,w;
    int vis[maxn],fa[maxn];
    int ca;
    struct node
    {
        int u,v;
        double w;
    //    bool operator < (const node &x) const{
    //        return w < x.w; //最小生成树 权值小到大
    //    }
    }e[maxn<<1];
    bool cmp(node a,node b)
    {
        return a.w<b.w;
    }
    int Find(int x)
    {
        return x==fa[x]?x:Find(fa[x]);
    }
    double get(int i,int j)
    {
        return sqrt(double(sqr(x[i]-x[j]))+double(sqr(y[i]-y[j])));
    }
    int main()
    {
        ca=1;
        while(~scanf("%d",&n),n) //n个点
        {
            m=0;
    
            for(int i=1;i<=n;i++) fa[i]=i;
            for(int i=1;i<=n;i++)
                scanf("%lf%lf",&x[i],&y[i]);
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    e[m].u=i;
                    e[m].v=j;
                    e[m++].w=get(i,j);
                }
            }
            double ans=0.0;
            sort(e,e+m,cmp); //m条边
            for(int i=0;i<m;i++)
            {
                int fx=Find(e[i].u);
                int fy=Find(e[i].v);
                if(fx!=fy)
                {
                    fa[fx]=fy;
                    ans = e[i].w;
                    if(Find(1)==Find(2))
                       break;
                }
            }
            printf("Scenario #%d
    Frog Distance = %.3f
    
    ",ca++,ans);
        }
    }
    /*
    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
    【题意】
    
    【类型】
    kruskal
    
    【分析】
    在kruskal的时候不断地添加边,直到第一个点和第二个点被添加在一起
    把边值排序,然后依次加入加入最小边,起点和终点一旦连通,那么解就是这条边了
    
    【时间复杂度&&优化】
    
    【trick】
    
    【数据】
    
    */
    
    /*
    每次选取不成环的最小边,
    直到这棵树选取了通往终点的最小边,那么最后选择的这条边必然是在树中最大的一条边,而且在其余的边中是最小的。
    你不会找到比这条边小的最大距离,
    因为比它小的最小距离都在树里了,而未选取该边前树中不包含终点,
    即比该边小的所有边无法到达终点。即改边满足的两个条件,最小,
    而且是起点到终点的最大距离
    */
    kruskal
  • 相关阅读:
    第三次上机作业
    第二次实训作业
    java第二次作业
    java程序设计第一次作业
    实训作业1
    java2
    我的第一次java作业
    第六次实训作业异常处理
    事件处理程序
    实训作业4
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9442392.html
Copyright © 2011-2022 走看看