首页 > 代码库 > POJ-1700 Crossing River
POJ-1700 Crossing River
Description
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won‘t be more than 1000 people and nobody takes more than 100 seconds to cross.Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.Sample Input
1 4 1 2 5 10Sample Output
17
每次决策后我们都发现过去了两个人。
于是n-=2;
再次循环。直到n<4;
代码:
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 int a[1200]; 7 int main(){ 8 int T; 9 cin>>T; 10 while(T--){ 11 int n; 12 cin>>n; 13 for(int i=1;i<=n;i++){ 14 scanf("%d",a+i); 15 } 16 sort(a+1,a+1+n); 17 int sum=0; 18 while(n){ 19 if(n==1){ 20 sum+=a[1]; 21 break; 22 } 23 else if(n==2){ 24 sum+=a[2]; 25 break; 26 } 27 else if(n==3){ 28 sum+=a[1]+a[2]+a[3]; 29 break; 30 } 31 else{ 32 sum+=min(a[2]+a[1]+a[n]+a[2],a[n]+a[1]+a[n-1]+a[1]); 33 n-=2; 34 } 35 } 36 cout<<sum<<endl; 37 } 38 return 0; 39 }
POJ-1700 Crossing River
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。