[UVA][模退火] 10228 - Star not a Tree@Morris' Blog|PChome Online 人新台
2014-03-28 08:13:31| 人5,294| 回1 | 上一篇 | 下一篇

[UVA][模退火] 10228 - Star not a Tree

0 收藏 0 0 站台

Problem E: 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 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.

The first line of input contains the number of case that you need to test followed by a blank line. Each test case start with 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.There is a blank line between each consecutive test case.

Output consists of one number, the total length of the cable segments, rounded to the nearest mm. Print a blank line between 2 consecutive test case.

Sample Input

1 4 0 0 0 10000 10000 10000 10000 0 

Output for Sample Input

28284 


目描述:


找一到所有距和最小。
四五入到整出距和。

目解法:

模退火法-冶金技,不地分子分散再聚合,,使得品越越好。

而退火就是冷,之後再加,如同一般。

用在此,退火意指往梯度(也就是好的位置)小的地方跑,至於加部分就有使用。

原本加是根某率函容忍跳到不好的地方,但是有可能跳出去又回原本的域最佳解。

其方法跟多撒後,再分退火的效用差不多,但是撒有,充性不。

由於少,有很,所以撒和多跑次,每次跳距逐小即可。

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
double distForAllPoints(double x, double y,
vector< pair<int, int> > &D) {
double sum = 0;
for(int i = D.size()-1; i >= 0; i--) {
sum += hypot(D[i].first - x, D[i].second - y);
}
return sum;
}
double randDouble() {
return (rand() % 32767) / 32767.0;
}
double annealing(vector< pair<int, int> > &D) {
#define S_MUL 0.6f
#define S_LEN 1000
#define T_CNT 10
#define E_CNT 10
double step = S_LEN;
double x[E_CNT], y[E_CNT], val[E_CNT];
double Lx, Ly, Rx, Ry, tx, ty, tcost;
Lx = Rx = D[0].first;
Ly = Ry = D[0].second;
for(int i = 0; i < D.size(); i++) {
Lx = min(Lx, (double)D[i].first);
Rx = max(Rx, (double)D[i].first);
Ly = min(Ly, (double)D[i].second);
Ry = max(Ry, (double)D[i].second);
}
for(int i = 0; i < E_CNT; i++) {
x[i] = randDouble() * (Rx - Lx) + Lx;
y[i] = randDouble() * (Ry - Ly) + Ly;
val[i] = distForAllPoints(x[i], y[i], D);
}
while(step > 0.1) {
for(int i = 0; i < E_CNT; i++) {
for(int j = 0; j < T_CNT; j++) {
tx = x[i] + randDouble() * 2 *step - step;
ty = y[i] + randDouble() * 2 * step - step;
tcost = distForAllPoints(tx, ty, D);
if(tcost < val[i]) {
val[i] = tcost, x[i] = tx, y[i] = ty;
}
}
}
step *= S_MUL;
}
double ret = val[0];
for(int i = 0; i < E_CNT; i++) {
ret = min(ret, val[i]);
}
printf("%.0lf\n", ret);
}
int main() {
int testcase, N;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &N);
vector< pair<int, int> > D;
int x, y;
for(int i = 0; i < N; i++) {
scanf("%d %d", &x, &y);
D.push_back(make_pair(x, y));
}
annealing(D);
if(testcase)
puts("");
}
return 0;
}

台: Morris
人(5,294) | 回(1)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][表] 12634 - Pairing Boys and Girls
此分上一篇:[POJ][(裸)笛卡] 1785 - Binary Search Heap Construction

Kasey
I do agree on some points <a href="https://testmyspeed.onl/">https://testmyspeed.onl/</a>
2020-09-13 16:41:56
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86