bzoj1857: [Scoi2010]传送带
一看就是三分
然后打个表看了一下 确实有三分性
由于轨迹是线性的所以目标函数应该是单峰的,虽然会比二次函数偏差一点
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<string> 7 8 using namespace std; 9 10 void setIO(const string& a) { 11 freopen((a+".in").c_str(), "r", stdin); 12 freopen((a+".out").c_str(), "w", stdout); 13 } 14 15 double ax, ay, bx, by, cx, cy, dx, dy, P, Q, R; 16 17 #include<cmath> 18 double dis(double x1, double y1, double x2, double y2) { 19 return hypot(x1 - x2, y1 - y2); 20 } 21 22 double calc(double x, double y) { 23 double x1 = cx, y1 = cy, x2 = dx, y2 = dy; 24 for(int i = 1; i <= 30; i++) { 25 double mx1 = x1 + (x2 - x1) / 3, my1 = y1 + (y2 - y1) / 3; 26 double mx2 = x1 + (x2 - x1) / 3 * 2, my2 = y1 + (y2 - y1) / 3 * 2; 27 double cost1 = dis(x, y, mx1, my1) / R + dis(mx1, my1, dx, dy) / Q; 28 double cost2 = dis(x, y, mx2, my2) / R + dis(mx2, my2, dx, dy) / Q; 29 if(cost1 > cost2) x1 = mx1, y1 = my1; 30 else x2 = mx2, y2 = my2; 31 } 32 return dis(x, y, x1, y1) / R + dis(x1, y1, dx, dy) / Q; 33 } 34 35 int main() { 36 37 cin >> ax >> ay >> bx >> by; 38 // if(ax > bx) swap(ax, bx), swap(ay, by); 39 cin >> cx >> cy >> dx >> dy; 40 // if(cx > dx) swap(cx, dx), swap(cy, dy); 41 cin >> P >> Q >> R; 42 43 double x1 = ax, y1 = ay, x2 = bx, y2 = by; 44 45 for(int i = 1; i <= 30; i++) { 46 double mx1 = x1 + (x2 - x1) / 3, my1 = y1 + (y2 - y1) / 3; 47 double mx2 = x1 + (x2 - x1) / 3 * 2, my2 = y1 + (y2 - y1) / 3 * 2; 48 double cost1 = dis(ax, ay, mx1, my1) / P + calc(mx1, my1); 49 double cost2 = dis(ax, ay, mx2, my2) / P + calc(mx2, my2); 50 if(cost1 > cost2) x1 = mx1, y1 = my1; 51 else x2 = mx2, y2 = my2; 52 } 53 54 printf("%.2f\n", dis(ax, ay, x1, y1) / P + calc(x1, y1)); 55 56 return 0; 57 }
原文出处http://www.cnblogs.com/showson/