Problem B: Jogging Trails

Problem B: Jogging Trails

时间: 1ms        内存:128M

描述:

Problem B: Jogging Trails

Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.

Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.

For each case, there should be one line of output giving the length of Gord's jogging route.

输入:

输出:

示例输入:

4 5
1 2 3
2 3 4
3 4 5
1 4 10
1 3 12
0

示例输出:

41

提示:

参考答案(内存最优[752]):

#include <math.h>
#include <stdio.h>
#define pi (2*acos(0))

double D,V,d,delta;

int i,j,k;

double v(double d){
   double R = D/2;
   double r = d/2;
   double vCyl = D * pi * R * R;
   double vcyl = d * pi * r * r;
   double vCone = pi * (D/2)*(D/2)*(D/2) / 3;
   double vcone = pi * (d/2)*(d/2)*(d/2) / 3;
   double res = vCyl - 2*vCone - vcyl + 2*vcone;
   return res;
}

main(){
   while (2 == scanf("%lf%lf",&D,&V) && D) {
      delta = d = D/2;
      for (d=D/2; delta > .000000001; delta /=2) {
         if (v(d) < V) d -= delta;
         else d += delta;
      }
      printf("%0.3lf\n",d);
   }
}

参考答案(时间最优[0]):

#include <math.h>
#include <stdio.h>
#define pi (2*acos(0))

double D,V,d,delta;

int i,j,k;

double v(double d){
   double R = D/2;
   double r = d/2;
   double vCyl = D * pi * R * R;
   double vcyl = d * pi * r * r;
   double vCone = pi * (D/2)*(D/2)*(D/2) / 3;
   double vcone = pi * (d/2)*(d/2)*(d/2) / 3;
   double res = vCyl - 2*vCone - vcyl + 2*vcone;
   return res;
}

main(){
   while (2 == scanf("%lf%lf",&D,&V) && D) {
      delta = d = D/2;
      for (d=D/2; delta > .000000001; delta /=2) {
         if (v(d) < V) d -= delta;
         else d += delta;
      }
      printf("%0.3lf\n",d);
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注