Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 0.94 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ## king.cpp [c++]
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. /**
  8.  * This function computes the minimal time to return to the kingdom =).
  9.  */
  10.     int
  11. minTime(int N, vector<int> roadTime, vector<int> flightTime, int K)
  12. {
  13.     if (N < 0)
  14.     {
  15.         return 0;
  16.     }
  17.     // one shouldn't fly if we can't anymore or needn't
  18.     int const minWithRoad = minTime(N - 1, roadTime, flightTime, K);
  19.     if (K == 0 || roadTime[N - 1] < flightTime[N - 1])
  20.     {
  21.         return minWithRoad + roadTime[N - 1];
  22.     }
  23.     int const minWithFlight = minTime(N - 1, roadTime, flight, K - 1);
  24.     return min(minWithRoad + roadTime[N - 1],
  25.             minWithFlight + flightTime[N - 1]);
  26. }
  27.  
  28. // just a simple test
  29.     int
  30. main()
  31. {
  32.     vector<int> u;
  33.     vector<int> v;
  34.     u.push_back(100);
  35.     u.push_back(100);
  36.     v.push_back(1);
  37.     v.push_back(1);
  38.     cout << minTime(2, u, v, 1) << endl; // '101'
  39. }