24h購物| | PChome| 登入
2014-03-28 08:13:31| 人氣5,147| 回應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,147) | 回應(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
詳全文