lelouche29

Toll_gate_Samsung

Sep 13th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. /*
  2. TESTED AND VERIFIED
  3. INPUT:-
  4. 3
  5. 7            
  6. 10 100
  7. 70 5
  8. 80 15
  9. 20 60
  10. 50 90
  11. 30 80
  12. 10 10
  13. 9
  14. 600 800
  15. 300 400
  16. 300 400
  17. 1000 400
  18. 300 600
  19. 100 300
  20. 600 300
  21. 600 500
  22. 1000 300
  23. 11
  24. 1000 10
  25. 700 900
  26. 400 500
  27. 300 10
  28. 900 900
  29. 300 10
  30. 50 900
  31. 50 900
  32. 700 900
  33. 500 900
  34. 50 10
  35.  
  36. OUTPUT>-
  37. 150
  38. 3300
  39. 2370
  40.  
  41. */
  42.  
  43. #include<iostream>
  44.  
  45. using namespace std;
  46.  
  47. struct p{
  48.     int cost,men;
  49. }toll[100];
  50.  
  51. int n;
  52. int ans;
  53.  
  54. void solve(int cur_toll, int bt1,int bt2, int bt3, int cost ){
  55.     //optimization
  56.     if(cost > ans)return;
  57.  
  58.     //base case when we have covered all toll booths
  59.     if(cur_toll==n){
  60.         ans=min(ans,cost);
  61.         return;
  62.     }
  63.  
  64.     //paying the toll
  65.     solve(cur_toll + 1, bt1, bt2, bt3, cost + toll[cur_toll].cost);              
  66.     //buying the men at the toll by paying double cost
  67.     solve(cur_toll + 1, bt1, bt2, bt3 + toll[cur_toll].men, cost + 2*toll[cur_toll].cost);          
  68.  
  69.     //total men available for fighting
  70.     int total_men= bt1 + bt2 + bt3;    
  71.                      
  72.     //fighting the men at toll to not pay the toll tax
  73.     if(total_men > toll[cur_toll].men){                                                    
  74.         if( toll[cur_toll].men > bt1 + bt2) bt3 = total_men - toll[cur_toll].men;
  75.         if(toll[cur_toll].men > bt1) bt2= (toll[cur_toll].men >= bt1 +bt2) ? 0 : bt2 - toll[cur_toll].men + bt1;
  76.         solve(cur_toll + 1, bt2, bt3, 0, cost);
  77.     }
  78. }
  79.  
  80.  
  81. int main(){
  82.     int t;
  83.     cin>>t;
  84.     while(t--){
  85.         //no of tolles
  86.         cin>>n;
  87.  
  88.         for(int i=0; i<n; i++)
  89.             cin>>toll[i].men>>toll[i].cost;
  90.  
  91.         ans=999999;
  92.         solve(0,0,0,0,0);
  93.         cout<<ans<<endl;
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment