POJ 2253 Frogger floyd算法

题目:click here

题意:

  给出两只青蛙的坐标A、B,和其他的n-2个坐标,任意两坐标间是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。

分析:

  最小化最大值--------

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring> 
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <string> 
 7 #define power(a) ((a)*(a))
 8 using namespace std;
 9 const int INF = 1e9+7;
10 const int M = 205;
11 
12 struct node { double x, y; } point[M];  // 点的坐标
13 int n, tcase;
14 double cost[M][M];  // 邻接矩阵存图
15 double dis( node a, node b )    {   // 距离
16     return sqrt( power(a.x-b.x) + power(a.y-b.y) );
17 }
18 
19 void floyd()    {   // 弗洛伊德算法
20     for( int k=1; k<=n; k++ )
21         for( int i=1; i<=n; i++ )
22             for( int j=1; j<=n; j++ )
23                 cost[i][j] = min( cost[i][j], max(cost[i][k],cost[k][j]) );
24 }
25 
26 void solve()    {
27     for( int i=1; i<=n; i++ )
28         scanf("%lf%lf", &point[i].x, &point[i].y );
29     for( int i=1; i<=n; i++ )   {
30         cost[i][i] = 0;
31         for( int j=i+1; j<=n; j++ ) {
32             double d = dis( point[i], point[j] );
33             cost[i][j] = d;                // 建立无向图
34             cost[j][i] = d;
35         }
36     }
37     floyd();
38     printf("Scenario #%d\n", tcase++);
39     printf("Frog Distance = %.3f\n\n", cost[1][2] );    
40 }
41 int main() {
42     tcase = 1;
43     while( ~scanf("%d", &n ) && n )  {
44         solve();
45     }        
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/TaoTaoCome/p/4755940.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值