SRM 146 DIV2 1000
Problem Statement |
|||||||||||||
A well-known riddle goes like this: Four people are crossing an old bridge. The bridge cannot hold more than two people at once. It is dark, so they can't walk without a flashlight, and they only have one flashlight! Furthermore, the time needed to cross the bridge varies among the people in the group. For instance, let's say that the people take 1, 2, 5 and 10 minutes to cross the bridge. When people walk together, they always walk at the speed of the slowest person. It is impossible to toss the flashlight across the bridge, so one person always has to go back with the flashlight to the others. What is the minimum amount of time needed to get all the people across the bridge? In this instance, the answer is 17. Person number 1 and 2 cross the bridge together, spending 2 minutes. Then person 1 goes back with the flashlight, spending an additional one minute. Then person 3 and 4 cross the bridge together, spending 10 minutes. Person 2 goes back with the flashlight (2 min), and person 1 and 2 cross the bridge together (2 min). This yields a total of 2+1+10+2+2 = 17 minutes spent. You want to create a computer program to help you solve new instances of this problem. Given an int[] times, where the elements represent the time each person spends on a crossing, your program should return the minimum possible amount of time spent crossing the bridge. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | In an optimal solution, exactly two people will be sent across the bridge with the flashlight each time (if possible), and exactly one person will be sent back with the flashlight each time. In other words, in an optimal solution, you will never send more than one person back from the far side at a time, and you will never send less than two people across to the far side each time (when possible). | ||||||||||||
Constraints |
|||||||||||||
- | times will have between 1 and 6 elements, inclusive. | ||||||||||||
- | Each element of times will be between 1 and 100, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
开始时从right中取得最小值回到left使用Arrays.sort排序后选择rigth[0],显然会对数组顺序产生影响,导致后续遍历出错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.*; 2 import java.util.regex.*; 3 import java.text.*; 4 import java.math.*; 5 import java.awt.geom.*; 6 7 public class BridgeCrossing { 8 private int dfs(int[] left, int[] right) { 9 if (left.length <= 2) { 10 Arrays.sort(left); 11 return left[left.length - 1]; 12 } 13 int min = Integer.MAX_VALUE; 14 for (int i = 0; i < left.length - 1; i++) { 15 for (int j = i + 1; j < left.length; j++) { 16 int[] lt = new int[left.length - 1]; 17 int p = 0; 18 int[] rt = new int[right.length + 1]; 19 int q = 0; 20 p = 0; 21 for (int k = 0; k < left.length; k++) { 22 if (k != i && k != j) { 23 lt[p] = left[k]; 24 p++; 25 } 26 } 27 right[right.length - 1] = left[i]; 28 right[right.length - 2] = left[j]; 29 int tmp = left[i] > left[j] ? left[i] : left[j]; 30 int lm = 0; 31 for (int k = 1; k < right.length; k++) { 32 if (right[k] < right[lm]) 33 lm = k; 34 } 35 tmp += right[lm]; 36 lt[p] = right[lm]; 37 q = 0; 38 for (int k = 0; k < right.length; k++) { 39 if (lm != k) { 40 rt[q] = right[k]; 41 q++; 42 } 43 } 44 45 tmp += dfs(lt, rt); 46 if (tmp < min) 47 min = tmp; 48 } 49 } 50 return min; 51 } 52 53 public int minTime(int[] times) { 54 return dfs(times, new int[2]); 55 } 56 }
posted on 2014-04-10 11:26 PolarisSherlock 阅读(137) 评论(0) 编辑 收藏 举报