006cf643bb 2013-06-22 kinaba: #include <iostream> 006cf643bb 2013-06-22 kinaba: #include <sstream> 006cf643bb 2013-06-22 kinaba: #include <iomanip> 006cf643bb 2013-06-22 kinaba: #include <vector> 006cf643bb 2013-06-22 kinaba: #include <string> 006cf643bb 2013-06-22 kinaba: #include <map> 006cf643bb 2013-06-22 kinaba: #include <set> 006cf643bb 2013-06-22 kinaba: #include <algorithm> 006cf643bb 2013-06-22 kinaba: #include <numeric> 006cf643bb 2013-06-22 kinaba: #include <iterator> 006cf643bb 2013-06-22 kinaba: #include <functional> 006cf643bb 2013-06-22 kinaba: #include <complex> 006cf643bb 2013-06-22 kinaba: #include <queue> 006cf643bb 2013-06-22 kinaba: #include <stack> 006cf643bb 2013-06-22 kinaba: #include <cmath> 006cf643bb 2013-06-22 kinaba: #include <cassert> 006cf643bb 2013-06-22 kinaba: using namespace std; 006cf643bb 2013-06-22 kinaba: typedef long long LL; 006cf643bb 2013-06-22 kinaba: typedef long double LD; 006cf643bb 2013-06-22 kinaba: typedef complex<double> CMP; 006cf643bb 2013-06-22 kinaba: 006cf643bb 2013-06-22 kinaba: class TravelOnMars { public: 006cf643bb 2013-06-22 kinaba: int minTimes(vector <int> range, int startCity, int endCity) 006cf643bb 2013-06-22 kinaba: { 006cf643bb 2013-06-22 kinaba: int N = range.size(); 006cf643bb 2013-06-22 kinaba: 006cf643bb 2013-06-22 kinaba: set<int> V; 006cf643bb 2013-06-22 kinaba: V.insert(startCity); 006cf643bb 2013-06-22 kinaba: for(int s=0 ;; ++s) 006cf643bb 2013-06-22 kinaba: { 006cf643bb 2013-06-22 kinaba: if(V.count(endCity)) 006cf643bb 2013-06-22 kinaba: return s; 006cf643bb 2013-06-22 kinaba: set<int> VV; 006cf643bb 2013-06-22 kinaba: for(set<int>::iterator it=V.begin(); it!=V.end(); ++it) 006cf643bb 2013-06-22 kinaba: { 006cf643bb 2013-06-22 kinaba: int v = *it; 006cf643bb 2013-06-22 kinaba: for(int d=0; d<=range[v]; ++d) { 006cf643bb 2013-06-22 kinaba: VV.insert((v+d)%N); 006cf643bb 2013-06-22 kinaba: VV.insert((v-d+N*100)%N); 006cf643bb 2013-06-22 kinaba: } 006cf643bb 2013-06-22 kinaba: } 006cf643bb 2013-06-22 kinaba: V.swap(VV); 006cf643bb 2013-06-22 kinaba: } 006cf643bb 2013-06-22 kinaba: } 006cf643bb 2013-06-22 kinaba: }; 006cf643bb 2013-06-22 kinaba: 006cf643bb 2013-06-22 kinaba: // BEGIN CUT HERE 006cf643bb 2013-06-22 kinaba: #include <ctime> 006cf643bb 2013-06-22 kinaba: double start_time; string timer() 006cf643bb 2013-06-22 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 006cf643bb 2013-06-22 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 006cf643bb 2013-06-22 kinaba: { os << "{ "; 006cf643bb 2013-06-22 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 006cf643bb 2013-06-22 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 006cf643bb 2013-06-22 kinaba: void verify_case(const int& Expected, const int& Received) { 006cf643bb 2013-06-22 kinaba: bool ok = (Expected == Received); 006cf643bb 2013-06-22 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 006cf643bb 2013-06-22 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 006cf643bb 2013-06-22 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 006cf643bb 2013-06-22 kinaba: #define END verify_case(_, TravelOnMars().minTimes(range, startCity, endCity));} 006cf643bb 2013-06-22 kinaba: int main(){ 006cf643bb 2013-06-22 kinaba: 006cf643bb 2013-06-22 kinaba: CASE(0) 006cf643bb 2013-06-22 kinaba: int range_[] = {2,1,1,1,1,1}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 1; 006cf643bb 2013-06-22 kinaba: int endCity = 4; 006cf643bb 2013-06-22 kinaba: int _ = 2; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: CASE(1) 006cf643bb 2013-06-22 kinaba: int range_[] = {2,1,1,1,1,1}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 4; 006cf643bb 2013-06-22 kinaba: int endCity = 1; 006cf643bb 2013-06-22 kinaba: int _ = 3; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: CASE(2) 006cf643bb 2013-06-22 kinaba: int range_[] = {2,1,1,2,1,2,1,1}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 2; 006cf643bb 2013-06-22 kinaba: int endCity = 6; 006cf643bb 2013-06-22 kinaba: int _ = 3; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: CASE(3) 006cf643bb 2013-06-22 kinaba: int range_[] = {3,2,1,1,3,1,2,2,1,1,2,2,2,2,3}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 6; 006cf643bb 2013-06-22 kinaba: int endCity = 13; 006cf643bb 2013-06-22 kinaba: int _ = 4; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: CASE(4) 006cf643bb 2013-06-22 kinaba: int range_[] = {10,1,1,1,4,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 3; 006cf643bb 2013-06-22 kinaba: int endCity = 10; 006cf643bb 2013-06-22 kinaba: int _ = 3; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: CASE(5) 006cf643bb 2013-06-22 kinaba: int range_[] = {50,50}; 006cf643bb 2013-06-22 kinaba: vector <int> range(range_, range_+sizeof(range_)/sizeof(*range_)); 006cf643bb 2013-06-22 kinaba: int startCity = 0; 006cf643bb 2013-06-22 kinaba: int endCity = 1; 006cf643bb 2013-06-22 kinaba: int _ = 1; 006cf643bb 2013-06-22 kinaba: END 006cf643bb 2013-06-22 kinaba: } 006cf643bb 2013-06-22 kinaba: // END CUT HERE