HDU 3644 a chocolate manufacturer's problem (Simulated Annealing)

Source: Internet
Author: User

Reprinted please indicate the source, thank you http://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove

Question: an arbitrary polygon determines whether a circle with a radius of R can be placed.

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3644

At first, I thought it was a semi-flat transaction. I decided to check that the submission time and code length were not the same, but I submitted it again.

Later, he was promoted to science, and the concave polygon cannot be obtained in this way. The concave polygon may have no cores. It is obviously incorrect to calculate the core after moving inward to R.

There are not many points, so only simulated annealing is required...

To write this question, first you have to write a judgment point in the polygon. Because I used to write too many points, I plan to write another one, and then I am tossing about all kinds of errors for one night, line segments are always replaced by straight lines. With this judgment, we can start simulated annealing.

The key issue of simulated annealing is how to grasp the fire. At the beginning, I tried to select N points, the step size reduced by 1/10 each time, and the precision was controlled at 1e-3. Actually, TLE, I read so much online code. Then start various attempts to reduce the number of selected points. Various Wa and TLE languages...

A good acmer gave a set of data

4

0 0

0 2

2 2

2 0

1

Generally, if the accuracy is too high during the determination, this set of data will not be able to pass, and the accuracy of 1e-8 will not be changed to 1e-3. After a chaotic change, it can be said that it barely passed this set of data.

The screen was refreshed early in the morning, and various attempts were triggered. The screen was eventually flushed into 200 ms.

You can select up to 20 vertices. Each vertex takes 5 steps and the step size is 0.55. The main reason for the previous TLE is that the group of data is merged and Wa is used.

# Include <iostream> # include <fstream> # include <iomanip> # include <cstdio> # include <cstring> # include <algorithm> # include <cstdlib> # include <cmath> # include <set> # include <map> # include <queue> # include <stack> # include <string> # include <vector> # include <ctime> # include <sstream> # include <cassert> # define ll long # define EPS 1e-7 # define zero () FABS (a) <EPS # define INF 1 <30 # define N 20 # define PI ACOs (-1.0) using namespace STD; struct point {Double X, Y; double val;} p [100], TP [100], pre, cur; struct segment {point a, B ;}; int N; inline double xmul (point P0, Point P1, point P2) {return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p2.x-Snapshot X) * (p1.y-policy);} inline double dist (point P1, point P2) {return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);} // calculates the distance between a point and a line segment from inline double dist_point_seg (point P, point A, point B) {point T = P; T. X + =. y-b.y; T. Y + = B. X-a.x; If (xmul (A, T, P) * xmul (B, T, P)> EPS) return dist (P, A) + EPS <dist (p, B )? Dist (P, A): dist (p, B); else return FABS (xmul (P, A, B)/dist (a, B );} inline bool online (point P1, point P2, point P) {If (zero (xmul (P1, P2, p) & (P. x-p1.x) * (p. x-p2.x) <EPS & (P. y-p1.y) * (p. y-p2.y) <EPS) return true; return false;} inline bool sequence SS (segment S1, segment S2) {If (xmul (s1.a, s1. B, s2.a) * xmul (s1.a, s1. B, s2. B) <EPS) if (xmul (s2.a, s2. B, s1.a) * xmul (s2.a, s2. B, s1. B) <EPS) return true; return false;} inline bool par Allel (segment S1, segment S2) {return zero (s1.a. x-s1. B .x) * (s2.a. y-s2. B .y)-(s2.a. x-s2. B .x) * (s1.a. y-s1. B .y);} // determines whether the point is in the polygon inline bool in_polygon (point CEN) {int CNT = 0; Segment S, E; S. A = CEN; S. b. y = CEN. y; S. b. X = 20000.0; For (INT I = 0; I <n; I ++) {e. A = P [I]; E. B = P [I + 1]; If (online (P [I], p [I + 1], CEN) return false; If (zero (P [I]. y-P [I + 1]. y) continue; If (online (S. a, S. b, P [I]) {If (P [I]. y> P [I + 1]. y) CNT ++;} else if (on Line (S. a, S. b, P [I + 1]) {If (P [I + 1]. y> P [I]. y) CNT ++;} else if (processing SS (S, E) CNT ++;} return CNT & 1;} inline void get_min_dist (point & cur) {double ret = inf; For (INT I = 0; I <n; I ++) ret = min (Ret, dist_point_seg (cur, P [I], P [I + 1]); cur. val = ret;} int main () {Double R, best [105]; srand (Time (null); While (scanf ("% d", & N )! = EOF & N) {double Maxx = 0, Maxy = 0, Minx = inf, miny = inf; For (INT I = 0; I <n; I ++) {scanf ("% lf", & P [I]. x, & P [I]. y); Maxx = Maxx> P [I]. x? Maxx: P [I]. X; Maxy = Maxy> P [I]. Y? Maxy: P [I]. Y; Minx = Minx <p [I]. X? Minx: P [I]. X; miny = miny <p [I]. Y? Miny: P [I]. y;} p [N] = P [0]; scanf ("% lf", & R); int M = min (N, N ); for (INT I = 0; I <m; I ++) {TP [I]. X = (P [I]. X + P [I + 1]. x)/2; TP [I]. y = (P [I]. Y + P [I + 1]. y)/2; TP [I]. val = 0;} double step = SQRT (Maxx-Minx) * (Maxx-Minx) + (Maxy-miny) * (Maxy-miny)/2; bool flag = false; while (Step> 1e-3 &&! Flag) {for (INT I = 0; I <M &&! Flag; I ++) {for (Int J = 0; j <5 &&! Flag; j ++) {double angle = rand (); cur. X = TP [I]. X + step * Cos (angle); cur. y = TP [I]. Y + step * sin (angle); If (! In_polygon (cur) continue; get_min_dist (cur); If (cur. val + 1e-3> TP [I]. val) {TP [I] = cur; If (cur. val + 1e-3> r) Flag = true ;}} step * = 0.55;} puts (flag? "Yes": "no");} return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.