POJ 2420 模拟退火

Posted bigtom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2420 模拟退火相关的知识,希望对你有一定的参考价值。

A Star not a Tree?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9298   Accepted: 4022

Description

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to connect any number of computers together in a linear arrangement. Luke is particulary proud that he solved a nasty NP-complete problem in order to minimize the total cable length. 
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn‘t figure out how to re-enable forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub. 

Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won‘t move his computers. He wants to minimize the total length of cable he must buy.

Input

The first line of input contains a positive integer N <= 100, the number of computers. N lines follow; each gives the (x,y) coordinates (in mm.) of a computer within the room. All coordinates are integers between 0 and 10,000.

Output

Output consists of one number, the total length of the cable segments, rounded to the nearest mm.

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
", tuihuo());
96     return 0;
97 }
View Code

 




以上是关于POJ 2420 模拟退火的主要内容,如果未能解决你的问题,请参考以下文章

poj2420(模拟退火大法好)

POJ2420.A Star not a Tree?(模拟退火)

poj 2420 A Star not a Tree? —— 模拟退火

POJ 2420 模拟退火

poj 2420,模拟退火算法,费马点

poj-2420 A Star not a Tree?(模拟退火算法)