过河问题

 N个人要从河的一侧到另一次 一个船最多做2个人 这个船的速度受慢的人的影响。

输入一堆数,为每个人 的时间,怎么规划划船的总时间最少?
 
 
  
  
  
  
  1. #include <iostream> 
  2. #include <list> 
  3. #include <algorithm> 
  4. using namespace std; 
  5. void input(); 
  6. void sortlist(); 
  7. void show(); 
  8. void solution(int sel); 
  9. list<int> timelistA,timelistB; 
  10. int time; 
  11.  
  12. void main() 
  13.     input(); 
  14.     sortlist(); 
  15.     solution(0); 
  16.     cout<<endl<<"总用时"<<time<<"秒"<<endl<<endl; 
  17.  
  18. void solution(int sel) 
  19.     timelistA.sort(); 
  20.     timelistB.sort(); 
  21.  
  22.     if (timelistA.size()==0) 
  23.     { 
  24.         return
  25.     } 
  26.     if(0==sel)  // 左->右 
  27.     { 
  28.         if (timelistA.size()>1) 
  29.         { 
  30.             list<int >::iterator a=timelistA.begin(); 
  31.             list<int >::iterator b=timelistA.begin();b++; 
  32.          
  33.         timelistB.push_back(*a); 
  34.         cout<<*a; 
  35.         timelistA.erase(a); 
  36.         cout<<" "<<*b<<"过去"<<endl; 
  37.         timelistB.push_back(*b); 
  38.         time+=*b; 
  39.         timelistA.erase(b); 
  40.          
  41.         } 
  42.         else  
  43.         { 
  44.                 cout<<timelistA.front()<<" "<<"过去"<<endl; 
  45.                 timelistB.push_back(timelistA.front()); 
  46.                 time+=timelistA.front(); 
  47.                 timelistA.erase(timelistA.begin()); 
  48.              
  49.         } 
  50.         solution(1); 
  51.     } 
  52.     else if(1==sel) //右->左 回来 
  53.     { 
  54.     cout<<timelistB.front()<<"  回来"<<endl; 
  55.     timelistA.push_back(timelistB.front()); 
  56.     time+=timelistB.front(); 
  57.     timelistB.erase(timelistB.begin()); 
  58.      
  59.     solution(2); 
  60.     } 
  61.     else if (2==sel)//左->右 
  62.     { 
  63.         list<int >::iterator a=timelistA.end(); 
  64.         a--;  
  65.         list<int >::iterator b=a; 
  66.         b--; 
  67.         timelistB.push_back(*a); 
  68.         timelistB.push_back(*b); 
  69.         cout<<*a<<" "
  70.         cout<<*b<<" 过去"<<endl; 
  71.             time+=*a; 
  72.         timelistA.erase(a); 
  73.         timelistA.erase(b); 
  74.         solution(3); 
  75.     } 
  76.     else if (3==sel) 
  77.     { 
  78.  
  79.         cout<<timelistB.front()<<" 过来"<<endl; 
  80.             time+=timelistB.front(); 
  81.             timelistA.push_back(timelistB.front()); 
  82.         timelistB.erase(timelistB.begin()); 
  83.         solution(0); 
  84.     } 
  85.      
  86.  
  87.  
  88. void input() 
  89. {    
  90.     cout<<"input every one :"<<endl; 
  91.  
  92.     while(1) 
  93.     { 
  94.         int a; 
  95.         cin>>a; 
  96.         timelistA.push_back(a); 
  97.  
  98.         if (getchar()=='\n'
  99.         { 
  100.             break
  101.         } 
  102.     } 
  103.     cout<<endl; 
  104.  
  105. void sortlist() 
  106.     timelistA.sort(); 
  107.  
  108. void show() 
  109.     list<int>::iterator a=timelistA.begin(); 
  110.     for(;a!=timelistA.end();++a) 
  111.         cout<<*a<<"\t"
  112. }

你可能感兴趣的:(职场,影响,休闲,划船)