POJ-1700 Crossing River
Posted ISGuXing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章