1. 程式人生 > >poj 2420 A Star not a Tree?

poj 2420 A Star not a Tree?

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

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
求一點距離給定點的距離和最小,輸出最小值。 先隨機出幾十個答案,然後就開始在這些答案的周圍找是否存在更優的答案,並且隨著程式的進行,這個“周圍”所表示的範圍會越來越小。 g++ 會WA,要C++;
int n;
struct Point{
	double x,y;
	void read(){
		scanf("%lf%lf",&x,&y);
	}
	void Rand(){
		x=rand()%10000;
		y=rand()%10000;
	}
}a[110],p[55],t;
double d[55];
inline double dis(Point a,Point b){
	return sqrt(SQR(a.x-b.x)+SQR(a.y-b.y));
}

inline double dis(Point b){
	double ans=0;
	rep(i,0,n) ans+=dis(b,a[i]);
	return ans;
}

int main(){
	srand(49999);
	while(cin>>n){
		rep(i,0,n) a[i].read();
		rep(i,0,55){
			p[i].Rand();
			d[i]=dis(p[i]);
		}
		for(double step=1e6;step>=1e-6;step/=2){
			rep(i,0,55){
				rep(j,0,50){
					double tmp=rand();
					t.x=p[i].x+cos(tmp)*step;
					t.y=p[i].y+sin(tmp)*step;
					tmp=dis(t);
					if(tmp<d[i]){
						p[i].x=t.x,p[i].y=t.y;
						d[i]=tmp;
					}
				}
			}
		}
		double ans=d[0];
		rep(i,1,55) ans=min(ans,d[i]);
		printf("%.0lf\n",ans);
	}
	return 0;
}


相關推薦

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

cal lan clas double || http eps algorithm sqrt 題目:http://poj.org/problem?id=2420 給出 n 個點的坐標,求費馬點; 模擬退火上。 代碼如下: #include<iostream>

POJ-2420 A Star not a Tree? 計算幾何 模擬退火

POJ-2420 A Star not a Tree? 題意: 給定n個點, 找到一個點p. 使得p到所有點的距離之和最小. 分析: 模擬退火隨機產生圓心座標, 跑一遍模擬退火就行, 這題n的範圍是100, 莫名其妙re, 開1000就ac了. 程式碼: #include

poj:2420 A Star not a Tree?(模擬退火演算法)

這個題和poj2069  都是用%lf過不了  改成%f就能過 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network u

poj 2420 A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3568 Accepted: 1778 Description Luke wants to upgrade

poj 2420 A Star not a Tree?

題意:給出平面上N(<=100)個點,你需要找到一個這樣的點,使得這個點到N個點的距離之和儘可能小。輸出這個最小的距離和(四捨五入到最近的整數) 思路:三分求 三分演算法解決凸形或者凹形函式

POJ 2420 A Star not a Tree?(二維費馬點)

題目:就是求多邊形的費馬點,輸出最小的距離。 做法:隨機化變步長貪心法(模擬退火???) 首先隨機選出一點,我直接取了0,0 然後選定一個步長,往4個方向開始找,如果更優則繼續,否則降低步長,直到滿足題目要求精度 也可以8個方向等等 #include<iostre

2420 A Star not a Tree? 三分套三分 或 模擬退火

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to

poj2420 A Star not a Tree?

not std opened mat tree emp space cnblogs bits 傳送門:http://poj.org/problem?id=2420 【題解】 費馬點問題,模擬退火。%.0f神坑 # include <math.h> # inc

【POJ2420】A star not a tree?

string truct div clas urn return 模擬 cti get 蒟蒻開始學模擬退火…… 起初一直不肯學,因為畢竟玄學算法…… 哎呀玄學怎麽就沒用呢?對不對? #include<iostream> #include<cstdio&g

[POJ2420]A Star not a Tree?

logs scan inf const sum nbsp cst 隨機 del 來源: Waterloo Local 2002.01.26 題目大意: 找出$n$個點的費馬點。 思路: 模擬退火。 首先任取其中一個點(或隨機一個坐標)作為基準點,每次向四周找距

poj2420 A Star not a Tree? 【模擬退火】

題意:平面上給你n個點,讓你求一個點,到這n點的距離和最小 板子得模擬退火,係數也是很好控制,對數器和ACdream得程式碼跑了1884組樣例,結果只有一組不一樣,可是我得解更優啊, 交上去RE, 不想再改了, 真心感覺或者模擬退火演算法是個假得演算法,a不a多數靠運氣

A Star not a Tree?(費馬點問題+模擬退火演算法)

A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9377 Accepted: 4043 Description Luke wan

安裝maven時提示:NB: JAVA_HOME should point to a JDK not a JRE.

我是win7系統. 我的java jdk是預設安裝的. 環境變數裡,JAVA_HOME:C:\Program Files\Java\jdk1.7.0_80;,path:C:\Program Files\Java\jdk1.7.0_80\bin; 然後開啟win+R。cm

When is a nova not a nova? When a white dwarf and a brown dwarf collide

Using the Atacama Large Millimeter/submillimeter Array (ALMA) in Chile, the international team of astronomers, including workers from the Universities of

linux下配置maven後使用命令mvn報錯NB: JAVA_HOME should point to a JDK not a JRE解決方案

筆者在之前的linux環境配置中,安裝jdk使用都都是rpm和壓縮包都方式安裝,這次使用yum安裝了一個openjdk後,再配置完maven後發現,不能使用mvn -v命令,提示JAVA_HOME should point to a JDK not a JRE錯誤資訊。以下是錯誤截圖和解決方案。在網

路徑規劃: a starA星演算法詳解

本文版權歸原作者、譯者所有,我只是轉貼;如果侵害到您的權益,請聯絡我,我將刪除本文。Amit's A star Page中譯文譯序這篇文章很適合A*演算法的初學者,可惜網上沒找到翻譯版的。本著好東西不敢獨享的想法,也為了鍛鍊一下英文,本人譯了這篇文章。由於本人英文水平非常有限

TOMCAT啟動提示NB: JAVA_HOME should point to a JDK not a JRE解決

        本人使用的Tomcat版本為apache-tomcat-6.0.18(用的是解壓包),解壓路徑為E:\apache-tomcat-6.0.18\apache-tomcat-6.0.18\,我如果部署在myEclipse裡啟動可以正常啟動,當然也可以訪問我的工程

JAVA_HOME should point to a JDK not a JRE

或者是tomcat一閃而過 最近在用jprofiler,啟動會話時,總是提示標題的錯誤,看了幾遍JAVA_HOME沒有配錯,但是還是報這個錯,最終發現是在C:/jdk1.6後加了分號的原因,去掉分號就

poj 2104 C - Count on a tree

none oid log uniq sin aps urn sca amp #include<iostream> #include<cstdio> #include<algorithm> #include<set> #in

Uncaught TypeError: $(...).tree is not a function at HTMLDocument.<anonymous> ,原因是確實js文件

ima ldo 原因 cti 項目 tree bubuko 技術 文件 不管惦記那個頁面都是上面的截圖內容,百度很多,只有一個說的是js有問題,後來經過一番查找,確實是js問題,如下圖 因為在git提交的時候不小心刪掉了, 點擊項目右鍵,出現下圖,