POJ 1379 模拟退火
Posted bigtom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1379 模拟退火相关的知识,希望对你有一定的参考价值。
Run Away
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10634 | Accepted: 3177 |
Description
One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look completely harmless at the first sight. But when activated, they start to throw out very hot java, uh ... pardon, lava. Unfortunately, all known paths to the Center Room (where the Sarcophagus is) contain a trigger that activates the trap. The ACM were not able to avoid that. But they have carefully monitored the positions of all the holes. So it is important to find the place in the Large Room that has the maximal distance from all the holes. This place is the safest in the entire room and the archaeologist has to hide there.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing three integers X, Y, M separated by space. The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= M <= 1000. The numbers X and Yindicate the dimensions of the Large Room which has a rectangular shape. The number M stands for the number of holes. Then exactly M lines follow, each containing two integer numbers Ui and Vi (0 <= Ui <= X, 0 <= Vi <= Y) indicating the coordinates of one hole. There may be several holes at the same position.
Output
Print exactly one line for each test case. The line should contain the sentence "The safest point is (P, Q)." where P and Qare the coordinates of the point in the room that has the maximum distance from the nearest hole, rounded to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).
Sample Input
3 1000 50 1 10 10 100 100 4 10 10 10 90 90 10 90 90 3000 3000 4 1200 85 63 2500 2700 2650 2990 100
Sample Output
The safest point is (1000.0, 50.0). The safest point is (50.0, 50.0). The safest point is (1433.0, 1669.8).
Source
思路:
同POJ2420, 更改了下每次步长与X,Y值相关就过了,要不然死活过不了样例。
还有衰减系数取0.99过不了样例,取0.999可以。
过了样例就A了。
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cmath> 7 #include <string> 8 #include <cstring> 9 #include <algorithm> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <set> 14 #include <map> 15 #include <list> 16 #include <iomanip> 17 #include <cctype> 18 #include <cassert> 19 #include <bitset> 20 #include <ctime> 21 22 using namespace std; 23 24 #define pau system("pause") 25 #define ll long long 26 #define pii pair<int, int> 27 #define pb push_back 28 #define pli pair<ll, int> 29 #define pil pair<int, ll> 30 #define clr(a, x) memset(a, x, sizeof(a)) 31 32 const double pi = acos(-1.0); 33 const int INF = 0x3f3f3f3f; 34 const int MOD = 1e9 + 7; 35 const double EPS = 1e-9; 36 37 /* 38 #include <ext/pb_ds/assoc_container.hpp> 39 #include <ext/pb_ds/tree_policy.hpp> 40 using namespace __gnu_pbds; 41 #define TREE tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> 42 TREE T; 43 */ 44 45 int T, n; 46 struct Point { 47 double x, y; 48 Point () {} 49 Point (double x, double y) : x(x), y(y) {} 50 double dis(Point p) { 51 return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); 52 } 53 } p[1005]; 54 inline double get_p(double delta, double T) { 55 return exp(delta / T); 56 } 57 double cal(Point pp) { 58 double res = 1e18; 59 for (int i = 1; i <= n; ++i) { 60 double tres = pp.dis(p[i]); 61 res = min(res, tres); 62 } 63 return res; 64 } 65 const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; 66 double X, Y; 67 Point ans_p; 68 double tuihuo() { 69 double x = rand() % (int)(X + 1), y = rand() % (int)(Y + 1); 70 double res = cal(Point(x, y)), ans = res; 71 ans_p = Point(x, y); 72 double T = 10000 / min(X, Y), phi = 0.999; 73 for (int rep = 1; rep <= 100000; ++rep) { 74 int r = rand() % 4; 75 int deltax = dir[r][0], deltay = dir[r][1]; 76 double tox = x + deltax * T * X; 77 double toy = y + deltay * T * Y; 78 if (0 <= tox && tox <= X && 0 <= toy && toy <= Y) { 79 double tres = cal(Point(tox, toy)); 80 if (tres > res) { 81 x = tox, y = toy; 82 res = tres; 83 if (res > ans) { 84 ans = res; 85 ans_p = Point(x, y); 86 } 87 } else { 88 double delta = tres - res; 89 double prob = get_p(delta, T); 90 prob = max(0.1, prob); 91 if (rand() < prob * RAND_MAX) { 92 x = tox, y = toy; 93 res = tres; 94 } 95 } 96 } 97 T *= phi; 98 } 99 return ans; 100 } 101 int main() { 102 srand(19980305); 103 scanf("%d", &T); 104 while (T--) { 105 scanf("%lf%lf%d", &X, &Y, &n); 106 for (int i = 1; i <= n; ++i) { 107 scanf("%lf%lf", &p[i].x, &p[i].y); 108 } 109 tuihuo(); 110 printf("The safest point is (%.1f, %.1f). ", ans_p.x, ans_p.y); 111 } 112 return 0; 113 }
以上是关于POJ 1379 模拟退火的主要内容,如果未能解决你的问题,请参考以下文章