POJ 2420 模拟退火法

A Star not a Tree?

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3272   Accepted: 1664

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

在多边形内找一个点,使得到多边形各个点的距离和最小,显然是费马点,模拟退火法

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/3 16:37:48
File Name :8.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
struct Point{
	double x,y;
	Point(double _x=0,double _y=0){
		x=_x;y=_y;
	}
};
int dcmp(double x){
	if(fabs(x)<eps)return 0;
	return x<0?-1:1;
}
Point operator + (Point a,Point b){
	return Point(a.x+b.x,a.y+b.y);
}
Point operator - (Point a,Point b){
	return Point(a.x-b.x,a.y-b.y);
}
Point operator * (Point a,double p){
	return Point(a.x*p,a.y*p);
}
Point operator / (Point a,double p){
	return Point(a.x/p,a.y/p);
}
bool operator <(const Point &a,const Point &b){
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator == (const Point &a,const Point  &b){
	return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Point a,Point b){
	return a.x*b.x+a.y*b.y;
}
double Length(Point a){
	return sqrt(Dot(a,a));
}
double Angle(Point a,Point b){
	return acos(Dot(a,b)/Length(a)/Length(b));
}
double angle(Point a){
	return atan2(a.y,a.x);
}
double Cross(Point a,Point b){
	return a.x*b.y-a.y*b.x;
}
Point Rotate(Point a,double rad){
	return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Point GetLineIntersection(Point p,Point v,Point q,Point w){
	Point u=p-q;
	double t=Cross(w,u)/Cross(v,w);
	return p+v*t;
}
Point perpencenter(Point a,Point b,Point c){
	Point A,B,C,D;
	A=c;
	B.x=c.x-a.y+b.y;
	B.y=c.y+a.x-b.x;
	C=b;
	D.x=b.x-a.y+c.y;
	D.y=b.y+a.x-c.x;
	return GetLineIntersection(A,B-A,C,D-C);
}
Point pp[110];
int main(){
	int n;
	while(cin>>n){
		for(int i=1;i<=n;i++)scanf("%lf%lf",&pp[i].x,&pp[i].y);
		double ans=INF,step=0;
		Point u,v;
		u.x=u.y=v.x=v.y=0;
		for(int i=1;i<=n;i++){
			step+=fabs(pp[i].x)+fabs(pp[i].y);
			u.x+=pp[i].x;
			u.y+=pp[i].y;
		}
		u=u/n;
		for(;step>eps;step/=2){
			for(int i=-4;i<=4;i++)
				for(int j=-4;j<=4;j++){
					v.x=u.x+step*i;
					v.y=u.y+step*j;
					double cnt=0;
					for(int p=1;p<=n;p++)cnt+=Length(v-pp[p]);
					if(cnt<ans){
						ans=cnt;
						u=v;
					}
				}
		}
		int hh;
		double ss=ans-(int)ans;
		if(ss<0.5)hh=(int)ans;else hh=(int)ans+1;
		printf("%d\n",hh);
	}
}

POJ 2420 模拟退火法,布布扣,bubuko.com

时间: 2024-10-08 10:44:12

POJ 2420 模拟退火法的相关文章

POJ 1379 模拟退火法

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5631   Accepted: 1728 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 complet

Shuffle&#39;m Up (poj 3087 模拟)

Language: Default Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5968   Accepted: 2802 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting

三分 POJ 2420 A Star not a Tree?

题目传送门 1 /* 2 题意:求费马点 3 三分:对x轴和y轴求极值,使到每个点的距离和最小 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3f3f; 12 double x[MAXN], y[MAXN]; 13 i

POJ 2420 A Star not a Tree? (模拟退火)

题目地址:POJ 2420 今天在比赛遇到了这题..于是现场学了一下模拟退火.... 这题是先初始化为一个点,然后不断趋近距离和最短的点.还是挺简单的.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h&g

G - Shuffle&#39;m Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过

G - Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by start

模拟退火 (poj 2420, poj 2069)

模拟退火基本知识 其伪代码例如以下: Let s = s0 For k = 0 through k_max (exclusive): T := temperature(k / k_max) Pick a random neighbour, s_new := neighbour(s) If P(E(s), E(s_new), T) > random(0, 1), move to the new state: s := s_new Output: the final state s 样例: poj

POJ 2420

模拟退火算法.昨天看了PPT,原来模拟退火算法涉及到马尔什么链,开始理解,它其实就是一个关于抽样的问题.随机抽样,选取足够多的样本,然后逐步逼近.而在平面上,由于T的下降,使得逐渐缩小至一点.然后,就可以了. 算法: 在平面上随机选取一些点,当然这些点应当有一点相关的性吧.我猜的. 然后在这些点随机移动,若移动后更优,则移动,否则,留待下一次循环. #include <iostream> #include <cstdio> #include <cstring> #inc

unity, 模拟退后台

//simulateSwitchToBackground.cs using UnityEngine;using System.Collections;using System.Collections.Generic;public class simulateSwitchToBackground : MonoBehaviour {void sendApplicationPauseMessage(bool isPause){Transform[] transList= GameObject.Find

POJ 1016 模拟字符串

Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20396   Accepted: 6817 Description "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price