24h購物| | PChome| 登入
2013-06-09 17:07:01| 人氣1,648| 回應0 | 上一篇 | 下一篇

[UVA][dp] 1096 - The Islands

推薦 0 收藏 0 轉貼0 訂閱站台

Wen Chen is the captain of a rescue boat. One of his important tasks is to visit a group of islands once a day to check if everything is all right. Captain Wen starts from the west-most island, makes a pass to the east-most island visiting some of the islands, then makes a second pass from the east-most island back to the first one visiting the remaining islands. In each pass Captain Wen moves steadily east (in the first pass) or west (in the second pass), but moves as far north or south as he needs to reach the islands. The only complication is that there are two special islands where Wen gets fuel for his boat, so he must visit them in separate passes. Figure 7 shows the two special islands in pink (1 and 3) and one possible path Captain Wen could take.

epsfbox{p4791.eps}

Calculate the length of the shortest path to visit all the islands in two passes when each island's location and the identification of the two special islands are given.

Input 

The input consists of multiple test cases. The data for each case begins with a line containing 3 integers n (4$ le$n$ le$100), b1, and b2 (0 < b1, b2 < n - 1 and b1 $ neq$ b2), where n is the number of islands (numbered 0 to n - 1) and b1 and b2 are the two special islands. Following this, there are n lines containing the integer x - and y -coordinates of each island (0$ le$x, y$ le$2000), starting with island 0. No two islands have the same x -coordinate and they are in order from west-most to east-most (that is, minimum x -coordinate to maximum x -coordinate).

Input for the last case is followed by a line containing 3 zeroes.

Output 

For each case, display two lines. The first line contains the case number and the length of the shortest tour Captain Wen can take to visit all the islands, rounded and displayed to the nearest hundredth. The second line contains a space-separated list of the islands in the order that they should be visited, starting with island 0 and 1, and ending with island 0. Each test case will have a unique solution. Follow the format in the sample output.

Sample Input 

5 1 3 
1 3 
3 4 
4 1 
7 5 
8 3 
5 3 2 
0 10 
3 14 
4 7 
7 10 
8 12 
0 0 0

Sample Output 

Case 1: 18.18 
0 1 4 3 2 0 
Case 2: 24.30 
0 1 3 4 2 0


題目描述:
從最左邊的點出發,第一趟必須全部往右邊走,到達最右邊的點,第二趟全部往左走,回到原點。
兩趟要走過所有點。題目要求有兩個特殊點,兩個點必須在不同趟中被走過,不可在同一趟。

再者是輸出的時候,以 0 1 開頭的方式輸出,且只有唯一解

題目解法:

雖然題目要求全往右之後,全往左。但事實上可當作兩條路徑同時向右經過左所有點。
dp[i][j]
表示第一條路徑的最後一點 i, 第二條路徑的最後一點 j, 而下一個必須被加入的點一定是
max(i, j)+1,然後考慮這一點要接在第一條路徑之後,還是第二條路徑之後。

特別規定特殊點 b1 只能在第一條,b2 只能在第二條。


#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
double dp[105][105];
pair<int, int> dparg[105][105];
int main() {
int n, b1, b2, cases = 0;
int i, j, k;
double x[105], y[105];
while(scanf("%d %d %d", &n, &b1, &b2) == 3) {
if(n == 0) break;
for(i = 0; i < n; i++)
scanf("%lf %lf", &x[i], &y[i]);
double ret = 1e+60;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++)
dp[i][j] = 1e+60;
}
dp[0][0] = 0;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
k = max(i, j)+1;
if(k != b2) {
if(dp[i][k] > dp[i][j] + hypot(x[k]-x[j], y[k]-y[j])) {
dp[i][k] = dp[i][j] + hypot(x[k]-x[j], y[k]-y[j]);
dparg[i][k] = make_pair(i, j);
}
}
if(k != b1) {
if(dp[k][j] > dp[i][j] + hypot(x[k]-x[i], y[k]-y[i])) {
dp[k][j] = dp[i][j] + hypot(x[k]-x[i], y[k]-y[i]);
dparg[k][j] = make_pair(i, j);
}
}
}
}
for(i = 0; i < n; i++) {
ret = min(ret, dp[i][n-1] + hypot(x[n-1]-x[i], y[n-1]-y[i]));
ret = min(ret, dp[n-1][i] + hypot(x[n-1]-x[i], y[n-1]-y[i]));
}
printf("Case %d: %.2lf\n", ++cases, ret);
pair<int, int> pre;
for(i = 0; i < n; i++) {
if(fabs(dp[i][n-1] + hypot(x[n-1]-x[i], y[n-1]-y[i])-ret) < 1e-8) {
pre = make_pair(i, n-1);
break;
}
if(fabs(dp[n-1][i] + hypot(x[n-1]-x[i], y[n-1]-y[i])-ret) < 1e-8) {
pre = make_pair(n-1, i);
break;
}
}
int path1[105], path2[105], p1 = 0, p2 = 0;
while(true) {
i = pre.first, j = pre.second;
pre = dparg[i][j];
if(i != pre.first)
path1[p1++] = i;
else
path2[p2++] = j;
if(pre.first == 0 && pre.second == 0) {
break;
}
}
printf("0 ");
if(path2[p2-1] == 1) {
for(i = p2-1; i >= 0; i--)
printf("%d ", path2[i]);
for(i = 0; i < p1; i++)
printf("%d ", path1[i]);
} else {
for(i = p1-1; i >= 0; i--)
printf("%d ", path1[i]);
for(i = 0; i < p2; i++)
printf("%d ", path2[i]);
}
puts("0");
}
return 0;
}

台長: Morris
人氣(1,648) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dp] 1347 - Tour
此分類上一篇:[UVA][大數開根號] 10606 - Opening Doors

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文