joj2476: Star travel

 2476: Star travel


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 65536K 895 177 Standard

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 }

 

posted @ 2012-05-16 19:23  漂木  阅读(288)  评论(0编辑  收藏  举报