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

来源:互联网 发布:淘宝排名怎么靠前 编辑:程序博客网 时间:2024/06/12 00:59

A Star not a Tree?
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4377 Accepted: 2164

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

40 00 1000010000 1000010000 0

Sample Output

28284

题意:在一个范围内给出一些点,找出某一个点,使得这个点到其他所有点的距离之和最短,输出最短距离。

解题思路:在一个范围内求极值,没有其他通用算法时,自然想到模拟退火,越用越觉得它的强大。。。

代码:

#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>using namespace std;#define eps 1e-3#define inf 1e20#define L 10#define P 20#define PI acos(-1.0)#define maxn 110#define RAD 10000double x[maxn],y[maxn];double px[maxn],py[maxn],dis[maxn];int n;double random(){return (rand()%RAD+1)/(double)RAD;}double p_dis(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));}double calc(double p,double q){double sum = 0.0;for(int i=0;i<n;i++){sum += p_dis(p,q,x[i],y[i]);}return sum;}int main(){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lf %lf",&x[i],&y[i]);}//srand(time(NULL));double S = 10000.0;px[0] = 0, py[0]=0;px[1] = S, py[1]=0;px[2] = 0, py[2]=S;px[3] = S, py[3]=S;for(int i=0;i<P;i++){if(i>=4){px[i] = random()*S;py[i] = random()*S;}dis[i] = calc(px[i],py[i]);}double delta = S;while(delta>eps){for(int i=0;i<P;i++){for(int j=0;j<L;j++){double theta = random()*2*PI;double dx = px[i] + delta*cos(theta);double dy = py[i] + delta*sin(theta);double temp = calc(dx,dy);if(temp<dis[i]){dis[i] = temp;px[i] = dx;py[i] = dy;}}}delta *= 0.85;}double ans = inf;for(int i=0;i<P;i++){if(dis[i]<ans) ans = dis[i];}printf("%.0f\n",ans);return 0;}


0 0
原创粉丝点击