poj2253(dijkstra||dijkstra||floy…

http://poj.org/problem?id=2253

 

Frogger
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 17536Accepted: 5717

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个点,求第1个点到第2个点的所有路径中  每条路径上的路径上最大边  最小是多少。就是求最短路径的 最大值。
解题思路:http://blog.sina.com.cn/s/blog_64018c250100taa5.html

其实这道题说实在就是为floyd量身设计的,floyd算法中的dp[i][j]表示i到j的最短路,而且floyd的原理说到底就是动态规划,状态转移方程式为: dp[i][j] = max(dp[i][k] + dp[k][j] , dp[i][j]); 这道题要求的是从i到j的路径上的最大的出现过的边的权值,如果存在多条路径那么使得最后的结果尽可能小,其实想不明白的话可以类比于最小生成树的prim算法,只不过最小生成树要求每个顶点都被链接,而这里没有硬性规定,所以和最小生成树还是有区别的, 在这里运用到和floyd推导的类似的方法(不理解的话建议看一下算法导论):
只有构成从 i j 的左右路径dp[i][k] dp[k][j] 都小于 dp[i][j] ,这样才能用dp[i][k] dp[k][j] 替换掉 dp[i][j] 不然的话 由dp[i][j] 构成的路径中出现过的最大的权值肯定大于 dp[i][k] dp[k][j] 构成的路径中出现过的最大的权值

#include<stdio.h>
#include<math.h>
#include<string.h>
struct coo{
   double x;
   double y;
}st[205];
double map[205][205];
int vis[205];
double dis[205];
int N;
double dist(int i,int j)//求两点间的距离
{
 double x1,y1;
 x1=pow(st[j].x-st[i].x,2.0);
 y1=pow(st[j].y-st[i].y,2.0);
 return sqrt(x1+y1);
}
double max(double a,double b)//求最大值
{
 return a>b?a:b;
}
void dijkstra()
{
 int i,j,u;
 double min1;
    dis[1]=0;
 for(i=2;i<=N;i++)
 {
  dis[i]=map[1][i];
  vis[i]=0;
 }
 vis[1]=1;
 for(i=2;i<=N;i++)
 {
  min1=10000000;
  for(j=1;j<=N;j++)
  {
   if(vis[j]==0 && dis[j]<min1)
   {
    u=j;
    min1=dis[j];
   }
  }
  if(u==2)
   break;
  vis[u]=1;
  for(j=1;j<=N;j++)
  {
   if(vis[j]==0 && dis[j]>max(dis[u],map[u][j]))
   {
                   dis[j]=max(dis[u],map[u][j]);
   }
  }
 }
}
int main()
{
     int i,j;
  int Case=0;
  while(scanf("%d",&N)!=EOF && N!=0)//有几个点
  {
     for(i=1;i<=N;i++)
      scanf("%lf%lf",&st[i].x,&st[i].y);
  memset(map,0,sizeof(map));//清零
  for(i=1;i<=N;i++)
  {
   for(j=1;j<=N;j++)
   {
              map[i][j]=map[j][i]=dist(i,j);//构建邻接矩阵
   }
  }
  dijkstra();
  printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++Case,dis[2]);
  }
  return 0;
}

 

 

2012.9.5

//prim()算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXN 1100
int vis[MAXN];//标记访问
double lowcost[MAXN];//储存最小距离
double cost[MAXN][MAXN];//保存邻接矩阵
double x[MAXN],y[MAXN];//记录坐标
int n;
#define inf 0x3f3f3f3f
double maxx(double a,double b)
{
    return a>b?a:b;
}
void prim()
{
    double max=-10000;
    memset(vis,0,sizeof(vis));
    //int ans=0;
    for(int i=0;i<n;i++)
    {
        lowcost[i]=cost[0][i];
    }
    lowcost[0]=0;
    vis[0]=1;
    int min;
    int k;
    for(int i=0;i<n-1;i++)
    {
        min=inf;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&lowcost[j]<min)
            {
                min=lowcost[j];
                k=j;
            }
        }
       // if(k==1)
        //return ;
        max=max>lowcost[k]?max:lowcost[k];
        //ans+=lowcost[k];
        vis[k]=1;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&lowcost[j]>maxx(lowcost[k],cost[k][j]))//关键
            {
                lowcost[j]=maxx(lowcost[k],cost[k][j]);
            }
           
        }
    }
    //return max;
   // return ans;
}
int main()
{
    int cas=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                cost[i][j]=0;
                else
                cost[i][j]=inf;
            }
        }
        //scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                scanf("%lf %lf",&x[i],&y[i]);
        }
        for(i=0;i<n;i++)//求点与点之间的距离
        {
            for(j=0;j<n;j++)
            {
                cost[i][j]=(x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]);
                cost[j][i]=cost[i][j];
            }
        }
        prim();
        printf("Scenario #%d\n",cas++);
        printf("Frog Distance = %.3f\n\n",sqrt(1.0*lowcost[1]));
        //printf("%.3lf\n",sqrt(1.0*lowcost[1]));
    }
    return 0;
}

 

 

 

//dijkstra()

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int inf =0x7f7f7f7f;
#define maxn 1010
int map[maxn][maxn];
int dis[maxn];
int n;
struct point
{
    int x;
    int y;
}pnt[maxn];
int _dist(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int max(int a,int b)
{
    return a>b?a:b;
}
void dijkstra()
{
    int i,j,k;
    int min;
    int p[maxn];
    for(i=1;i<=n;i++)
    {
        p[i]=0;
        dis[i]=map[1][i];
    }
    dis[1]=0;
    p[1]=1;
    for(i=1;i<=n-1;i++)
    {
        min=inf;
        for(j=1;j<=n;j++)
        {
            if(!p[j]&&dis[j]<min)
            {
                min=dis[j];
                k=j;
            }
        }
        if(k==0)//剪枝
        return;
        p[k]=1;
        for(j=1;j<=n;j++)
        {
            if(!p[j]&&dis[j]>max(dis[k],map[k][j]))//关键
            {
                dis[j]=max(dis[k],map[k][j]);
            }
        }
    }
}
int main()
{
    int cas=1;
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&pnt[i].x,&pnt[i].y);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(i==j)
                map[i][j]=0;
                else
                map[i][j]=inf;
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                map[i][j]=_dist(pnt[i],pnt[j]);
            }
        }
        dijkstra();
        printf("Scenario #%d\n",cas++);

        printf("Frog Distance = %.3f\n\n",sqrt(1.0*dis[2]));
        //printf("Scenaria #%d\n",cas++);
        //printf("Frog Distance = %.3f\n\n",sqrt(1.0*dis[2]));
    }
    return 0;
}

 

 

//floyd()算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAXN 1100
int vis[MAXN];//标记访问
double lowcost[MAXN];//储存最小距离
double cost[MAXN][MAXN];//保存邻接矩阵
double x[MAXN],y[MAXN];//记录坐标
int n;
#define inf 0x3f3f3f3f
double maxx(double a,double b)
{
    return a>b?a:b;
}
void floyd()
{
    int i,j,k;

    double temp;
   
    for(k=0;k<n;k++)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                //temp=cost[i][k]>cost[k][j]?cost[i][k]:cost[k][j];
                //if(temp<cost[i][j])
                //cost[i][j]=cost[j][i]=temp;
                if(cost[i][j]>cost[i][k]+cost[k][j]);
                cost[i][j]>cost[i][k]+cost[k][j];
            }
        }
    }
}
int main()
{
    int cas=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                cost[i][j]=0;
                else
                cost[i][j]=inf;
            }
        }
        //scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                scanf("%lf %lf",&x[i],&y[i]);
        }
        for(i=0;i<n;i++)//求点与点之间的距离
        {
            for(j=0;j<n;j++)
            {
                cost[i][j]=(x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]);
                cost[j][i]=cost[i][j];
            }
        }
        floyd();
        //prim();
        printf("Scenario #%d\n",cas++);
        printf("Frog Distance = %.3f\n\n",sqrt(1.0*cost[0][1]));
        //printf("%.3lf\n",sqrt(1.0*lowcost[1]));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值