POJ 1379 Run Away (Basic simulated annealing)

Source: Internet
Author: User

Question: find out one point. The shortest distance from all points is the maximum.
 
Http://poj.org/problem? Id = 1379
 
 
Randomization step size, greedy adjustment.
 
What we need to prove with Simulated Annealing ~~~~ % # @ & % ...... @ % ¥ % @ Proof complete
 
I have nothing to say. The time limit and accuracy requirements are not high. Just do it.
 
 
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <cmath>
# Include <string>
# Include <vector>
# Include <algorithm>
# Include <map>
# Include <set>
# Include <ctime>
# Define maxn200005
# Define eps 1e-8
# Define inf 2000000000
# Define LL long
# Define zero (a) fabs (a) <eps
# Define MOD 19901014.
# Define N 1000005
# Define pi acos (-1.0)
Using namespace std;
Int t, n;
Double X, Y;
Double best [50];
Struct Point {
Double x, y;
Bool check (){
If (x>-eps & x <eps + X & y>-eps & y <eps + Y)
Return true;
Return false;
}
} P [2, 1005], tp [50];
Double dist (Point p1, Point p2 ){
Return sqrt (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Double slove (Point p0 ){
Double ans = inf;
For (int I = 0; I <n; I ++)
Ans = min (ans, dist (p [I], p0 ));
Return ans;
}
Int main (){
Scanf ("% d", & t );
Srand (time (NULL ));
While (t --){
Scanf ("% lf % d", & X, & Y, & n );
For (int I = 0; I <n; I ++)
Scanf ("% lf", & p [I]. x, & p [I]. y );
For (int I = 0; I <15; I ++ ){
Tp [I]. x = (rand () % 1000 + 1)/1000.0 * X;
Tp [I]. y = (rand () % 1000 + 1)/1000.0 * Y;
Best [I] = slove (tp [I]);
}
Double step = max (X, Y)/sqrt (1.0 * n );
While (step> 1e-3 ){
For (int I = 0; I <15; I ++ ){
Point cur, pre = tp [I];
For (int j = 0; j <35; j ++ ){
Double angle = (rand () % 1000 + 1)/1000.0*2 * pi;
Cur. x = pre. x + cos (angle) * step;
Cur. y = pre. y + sin (angle) * step;
If (! Cur. check () continue;
Double tmp = slove (cur );
If (tmp> best [I]) {
Tp [I] = cur;
Best [I] = tmp;
}
}
}
Step * = 0.85;
}
Int idx = 0;
Double ans = 0;
For (int I = 0; I <15; I ++ ){
If (best [I]> ans ){
Ans = best [I];
Idx = I;
}
}
Printf ("The safest point is (%. 1f, %. 1f). \ n", tp [idx]. x, tp [idx]. y );
}
Return 0;
}
 
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <cmath>
# Include <string>
# Include <vector>
# Include <algorithm>
# Include <map>
# Include <set>
# Include <ctime>
# Define maxn200005
# Define eps 1e-8
# Define inf 2000000000
# Define LL long
# Define zero (a) fabs (a) <eps
# Define MOD 19901014.
# Define N 1000005
# Define pi acos (-1.0)
Using namespace std;
Int t, n;
Double X, Y;
Double best [50];
Struct Point {
Double x, y;
Bool check (){
If (x>-eps & x <eps + X & y>-eps & y <eps + Y)
Return true;
Return false;
}
} P [2, 1005], tp [50];
Double dist (Point p1, Point p2 ){
Return sqrt (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Double slove (Point p0 ){
Double ans = inf;
For (int I = 0; I <n; I ++)
Ans = min (ans, dist (p [I], p0 ));
Return ans;
}
Int main (){
Scanf ("% d", & t );
Srand (time (NULL ));
While (t --){
Scanf ("% lf % d", & X, & Y, & n );
For (int I = 0; I <n; I ++)
Scanf ("% lf", & p [I]. x, & p [I]. y );
For (int I = 0; I <15; I ++ ){
Tp [I]. x = (rand () % 1000 + 1)/1000.0 * X;
Tp [I]. y = (rand () % 1000 + 1)/1000.0 * Y;
Best [I] = slove (tp [I]);
}
Double step = max (X, Y)/sqrt (1.0 * n );
While (step> 1e-3 ){
For (int I = 0; I <15; I ++ ){
Point cur, pre = tp [I];
For (int j = 0; j <35; j ++ ){
Double angle = (rand () % 1000 + 1)/1000.0*2 * pi;
Cur. x = pre. x + cos (angle) * step;
Cur. y = pre. y + sin (angle) * step;
If (! Cur. check () continue;
Double tmp = slove (cur );
If (tmp> best [I]) {
Tp [I] = cur;
Best [I] = tmp;
}
}
}
Step * = 0.85;
}
Int idx = 0;
Double ans = 0;
For (int I = 0; I <15; I ++ ){
If (best [I]> ans ){
Ans = best [I];
Idx = I;
}
}
Printf ("The safest point is (%. 1f, %. 1f). \ n", tp [idx]. x, tp [idx]. y );
}
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.