joj2476: Star travel

 2476: Star travel


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s65536K895177Standard

In the future world, one can travel by spaceship from one star to another. Because of the long distance, the fuel consumption is not linear with distance. We should take more material within our spaceship, so the weight of total spaceship increased also. The fuel consumption F can be calculated by this formula.

F= a * d + b * d * d

Here, d is the distance between any two star, a and b are parameters.

Given the coordinates of each star, you should calculate the shortest path between two star. Here, shortest means the least fuel is used.

Input

The first line of each case is the number of stars n (2<=n<=20), and the double parameters a and b. The next n lines includes three double values that are x-axis, y-axis and z-axis coordinates. The next line is one integer m. There are two integers s and t in the next m lines, s and t is star number (from 1).

Output

For each question of each case, find out the shorest path between star s and t. Print the least fuel amount from star s to star t, rounded to 3 digit after the decimal point.

Sample Input

3 1.0 2.0
-12.008 82.268 174.260
50.033 1.675 108.423
211.106 241.439 41.528
2
3 2
2 1

Sample Output

176108.946
29478.812

 

Problem Source: skywind

 


This problem is used for contest: 101 


Submit / Problem List / Status / Discuss

图论的题,用到Floyd算法。
题目只是在求距离上绕了个小圈。
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 double dis[25][25];
 5 
 6 int main()
 7 {
 8     //freopen("in.txt", "r", stdin);
 9     
10     double a, b, d;
11     int n;
12     
13     while (scanf("%d %lf %lf", &n, &a, &b) == 3)
14     {
15         double x[n+1], y[n+1], z[n+1];
16         for (int i=1; i<=n; ++i)
17         {
18             scanf("%lf%lf%lf", &x[i], &y[i], &z[i]);
19         }
20         
21         for (int i=1; i<=n; ++i)
22         {
23             for (int j=1; j<=n; ++j)
24             {
25                 d = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) + (z[i]-z[j])*(z[i]-z[j]));
26                 dis[i][j] = a*d + b*d*d;
27             }
28         }
29         
30         //floyd算法
31         for (int k=1; k<=n; ++k)
32         {
33             for (int i=1; i<=n; ++i)
34             {
35                 for (int j=1; j<=n; ++j)
36                 {
37                     if (dis[i][k]+dis[k][j] < dis[i][j])
38                     {
39                         dis[i][j] = dis[i][k] + dis[k][j];
40                     }
41                 }
42             }
43         }
44         
45         scanf("%d", &n);
46         int i, j;
47         while (n--)
48         {
49             scanf("%d%d", &i, &j);
50             printf("%.3lf\n", dis[i][j]);
51         }
52     }
53     
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/RootJie/archive/2012/05/16/2504679.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值