標籤:des style class blog code http
A Star not a Tree?
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 3435 |
|
Accepted: 1724 |
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
Source
Waterloo Local 2002.01.26
題目大意:
求n邊形的費馬點,即找到一個點使得這個點到n個點的距離之和最小。
解題思路:
三角形也有費馬點,三角形費馬點是這樣定義的:尋找三角形內的一個點,使得三個頂點到該點的距離之和最小。
三角形費馬點的做法是:
(1)若有一個角大於120度,那麼這個角所在的點就是費馬點。
(2)若不存在,那麼對於三角形ABC,任取兩條邊(假設AB、AC),向外做等邊三角形得到C‘ 和 A‘ ,那麼AA‘ 和CC‘ 的交點就是費馬點。
那麼對於這題n多邊形,我採取的策略完全不同,採用了類比退火的做法,這種做法相對比較簡單,也可以用在求三角形費馬點之中。
類比退火演算法就跟數值演算法裡面的自適應演算法相同的道理
(1)定義好步長
(2)尋找一個起點,往8個方向的按這個步長搜尋。
(3)如果找到比答案更小的點,那麼以這個新的點為起點,重複(2)
(4)如果找不到比答案更小的點,那麼步長減半,再嘗試(2)
(5)直到步長小於要求的答案的精度就停止。
解題代碼:
#include <iostream>#include <cmath>#include <cstdio>using namespace std;const int offx[]={1,1,0,-1,-1,-1,0,1};const int offy[]={0,1,1,1,0,-1,-1,-1};const int maxn=110;const double eps=1e-2;struct point{ double x,y; point(double x0=0,double y0=0){x=x0;y=y0;} double getdis(point p){ return sqrt( (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y) ); }}p[maxn];int n;double getsum(point p0){ double sum=0; for(int i=0;i<n;i++) sum+=p0.getdis(p[i]); return sum;}void solve(){ for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); double ans=getsum(p[0]),step=100; point s=p[0],d; while(step>eps){ bool flag=false; for(int i=0;i<8;i++){ d=point(s.x+step*offx[i],s.y+step*offy[i]); double tmp=getsum(d); if(tmp<ans){ s=d; ans=tmp; flag=true; } } if(!flag) step/=2; } printf("%.0f\n",ans);}int main(){ while(scanf("%d",&n)!=EOF){ solve(); } return 0;}