poj 2253


floyd变形算法
设置D[i][j] = 从i到j的所有路径li,对于每条路径的每个边的权重w的最大值为liw,则D[i][j]为所有i到j的路径的liw最小的那条的liw值

测试数据:
lstar@ubuntu:~/ds/poj$ ./test_list.sh 2253
2253_1.in.dat:
2
0 0
3 4


3
17 4
19 4
18 5


0
result:


Scenario #1
Frog Distance = 5.000


Scenario #2
Frog Distance = 1.414


2253_2.in.dat:
2
0 0
3 4


3
17 4
19 4
18 5


8
1 1
4 0
1 2
2 2
3 2
4 2
3 0
5 1


3
9 10
10 10
100 10


6
5 5
100 100
4 4
3 3
2 2
1 1
5
1 2
2 1
3 2
4 1
5 2


3
999 999
1 1
3 3


0
result:


Scenario #1
Frog Distance = 5.000


Scenario #2
Frog Distance = 1.414


Scenario #3
Frog Distance = 1.414


Scenario #4
Frog Distance = 1.000


Scenario #5
Frog Distance = 134.350


Scenario #6
Frog Distance = 1.414


Scenario #7
Frog Distance = 1408.557


code:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <cstring>

  5. using namespace std;
  6. #define inf (10000.0)

  7. int n;
  8. #define MAX_V (200+1)
  9. struct _v{
  10.     int x;
  11.     int y;
  12. }V[MAX_V];

  13. double E[MAX_V][MAX_V];
  14. double D[MAX_V][MAX_V];
  15. double dist[MAX_V];
  16. int visit[MAX_V];

  17. double dis(int x1,int y1,int x2,int y2)
  18. {
  19.     return sqrt( (double)( (x2-x1)*(x2-x1)+ (y2-y1)*(y2 - y1)));
  20. }

  21. double floyd()
  22. {

  23.     
  24.     for(int k=1;k<=n;++k)
  25.     {
  26.         for(int i=1;i<=n;i++)
  27.         {
  28.             for(int j=1;j<=n;j++)
  29.             {
  30.                 //D[i][j] = max(D[i][k],D[k][j]);
  31.                 double w = max(D[i][k],D[k][j]);
  32.                 if(w < D[i][j])
  33.                     D[i][j] = w;
  34.             }
  35.         }
  36.     }

  37.     return D[1][2];

  38. }

  39. int index_v=1;
  40. int main()
  41. {
  42.     int x,y;
  43.     string blankline;
  44.     while(cin>>n)
  45.     {
  46.         //cout<<"n:"<<n<<endl;
  47.         if(n == 0)
  48.             break;

  49.         memset(V,0,sizeof(V));
  50.         memset(E,0,sizeof(E));
  51.         memset(visit,0,sizeof(visit));
  52.         memset(D,0,sizeof(D));

  53.         for(int i=1;i<=n;i++)
  54.         {
  55.             cin>>x>>y;
  56.             V[i].x=x;
  57.             V[i].y=y;
  58.         }
  59.         //create edge
  60.         for(int i=1;i<=n;i++)
  61.         {
  62.             for(int j=1;j<=n;j++)
  63.             {
  64.                 if( i!=j)
  65.                     E[i][j] = dis(V[i].x,V[i].y,V[j].x,V[j].y);
  66.                     D[i][j] = E[i][j];

  67.             }
  68.         }

  69.         //cout<<"create edge OK.."<<endl;
  70.         double d = floyd();
  71.         //floyd
  72.         cout<<"Scenario #"<<index_v++<<endl;
  73.         //cout<<"Frog Distance = "<<d<<endl;
  74.         printf("Frog Distance = %.3f\n",d);
  75.         cout<<endl;
  76.     }
  77. }




<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(16) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值