标签:sts local roo tree prepare ble kruskal ash play
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9298 | Accepted: 4022 |
Description
Input
Output
Sample Input
4 0 0 0 10000 10000 10000 10000 0
Sample Output
28284
Source
推荐博客:https://www.cnblogs.com/flashhu/p/8884132.html
实现:
T越大,步长和接受失败转移的概率越大。
此题中,初始温度10000, 衰减系数取0.999, 接收失败状态概率exp(-deltaf / T),重复100000次(又试了一下,取到6000就够了)。
另外,此题好像不加失败情况的转移也能过;还有衰减步长,在四个方向取最优转移的做法也能过。
此题数据较弱,不知道自己的退火模型好不好。
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 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[105]; 54 inline double get_p(double delta, double T) { 55 return exp(-delta / T); 56 } 57 double cal(Point pp) { 58 double res = 0; 59 for (int i = 1; i <= n; ++i) { 60 res += pp.dis(p[i]); 61 } 62 return res; 63 } 64 const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; 65 double tuihuo() { 66 int x = rand() % 10000, y = rand() % 10000; 67 double res = cal(Point(x, y)), ans = res; 68 double T = 10000, phi = 0.999; 69 for (int rep = 1; rep <= 100000; ++rep) { 70 int r = rand() % 4; 71 int deltax = dir[r][0], deltay = dir[r][1]; 72 double tres = cal(Point(x + deltax * T, y + deltay * T)); 73 if (tres < res) { 74 x += T * deltax, y += T * deltay; 75 res = tres; 76 ans = min(ans, res); 77 } else { 78 double delta = tres - res; 79 double prob = get_p(delta, T); 80 if (rand() < prob * RAND_MAX) { 81 x += T * deltax, y += T * deltay; 82 res = tres; 83 } 84 } 85 T *= phi; 86 } 87 return ans; 88 } 89 int main() { 90 srand(19980305); 91 scanf("%d", &n); 92 for (int i = 1; i <= n; ++i) { 93 scanf("%lf%lf", &p[i].x, &p[i].y); 94 } 95 printf("%.0f\n", tuihuo()); 96 return 0; 97 }
标签:sts local roo tree prepare ble kruskal ash play
原文地址:https://www.cnblogs.com/BIGTOM/p/9738626.html