Euclidean TSP
题目很难懂,就是给出两个公式,求c的最小值,有于关于c的函数单调性不是在1-100内单调的,所以不能用二分来逼近c的,所以就有了三分法(参考博客)
利用三分来逼近最小值
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int inf = 0x3f3f3f3f;
const int Max = 440;
typedef long long int LL;
double f1(double s, double c, double v)
{
return s*(1+1/c)/v;
}
double f2(double n, double c, double p)
{
return (n*pow(log2(n), c*sqrt(2))/(p*pow(10,9)));
}
int main()
{
double n, s, v, p, t;
while(cin>>n>>p>>s>>v)
{
double l = 0, r = 100;
while(r - l > 1e-7)
{
double mid1, mid2;
mid1 = (r+l)/2;
mid2 = (mid1 + r)/2;
double t1 = f1(s, mid1, v) + f2(n, mid1, p);
double t2 = f1(s, mid2, v) + f2(n, mid2, p);
if(t1 >= t2)
l = mid1;
else
r = mid2;
}
printf("%lf %lf\n", f1(s, l, v) + f2(n, l, p), l);
}
return 0;
}