
Untitled
By: a guest on
Aug 8th, 2012 | syntax:
None | size: 0.94 KB | hits: 3 | expires: Never
## king.cpp [c++]
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
/**
* This function computes the minimal time to return to the kingdom =).
*/
int
minTime(int N, vector<int> roadTime, vector<int> flightTime, int K)
{
if (N < 0)
{
return 0;
}
// one shouldn't fly if we can't anymore or needn't
int const minWithRoad = minTime(N - 1, roadTime, flightTime, K);
if (K == 0 || roadTime[N - 1] < flightTime[N - 1])
{
return minWithRoad + roadTime[N - 1];
}
int const minWithFlight = minTime(N - 1, roadTime, flight, K - 1);
return min(minWithRoad + roadTime[N - 1],
minWithFlight + flightTime[N - 1]);
}
// just a simple test
int
main()
{
vector<int> u;
vector<int> v;
u.push_back(100);
u.push_back(100);
v.push_back(1);
v.push_back(1);
cout << minTime(2, u, v, 1) << endl; // '101'
}