4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int INF = 0x3fffffff; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TaxiManager { public: 4fd800b3a8 2011-02-23 kinaba: int schedule(vector <string> roads, vector <string> customers) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int V = roads.size(); 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > d(V, vector<int>(V)); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<V; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<V; ++j) 4fd800b3a8 2011-02-23 kinaba: if( i != j ) 4fd800b3a8 2011-02-23 kinaba: d[i][j] = (roads[i][j]=='0' ? INF : roads[i][j]-'0'); 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<V; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<V; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<V; ++j) 4fd800b3a8 2011-02-23 kinaba: d[i][j] = min(d[i][j], d[i][k]+d[k][j]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: const int M = customers.size(); 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > c; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<M; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int f, t; 4fd800b3a8 2011-02-23 kinaba: stringstream(customers[i]) >> f >> t; 4fd800b3a8 2011-02-23 kinaba: c.push_back( make_pair(f,t) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int best = INF; 4fd800b3a8 2011-02-23 kinaba: for(int mask=0; mask<(1<<M); ++mask) 4fd800b3a8 2011-02-23 kinaba: best = min(best, max(tsp(d,c,mask), tsp(d,c,((1<<M)-1)&~mask))); 4fd800b3a8 2011-02-23 kinaba: return best; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<pair<int,int>, int> memo; 4fd800b3a8 2011-02-23 kinaba: int tsp(const vector< vector<int> >& d, const vector< pair<int,int> >& c, int mask, int cur=0) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( mask == 0 ) 4fd800b3a8 2011-02-23 kinaba: return d[cur][0]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<int,int> key(mask, cur); 4fd800b3a8 2011-02-23 kinaba: if( memo.count(key) ) return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int best = INF; 4fd800b3a8 2011-02-23 kinaba: for(int next=0; (1<<next)<=mask; ++next) 4fd800b3a8 2011-02-23 kinaba: if( (1<<next) & mask ) 4fd800b3a8 2011-02-23 kinaba: best = min(best, 4fd800b3a8 2011-02-23 kinaba: d[cur][c[next].first] + d[c[next].first][c[next].second] + tsp(d,c,mask&~(1<<next),c[next].second)); 4fd800b3a8 2011-02-23 kinaba: return memo[key] = best; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, TaxiManager().schedule(roads, customers));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"020200" 4fd800b3a8 2011-02-23 kinaba: ,"202020" 4fd800b3a8 2011-02-23 kinaba: ,"020002" 4fd800b3a8 2011-02-23 kinaba: ,"200020" 4fd800b3a8 2011-02-23 kinaba: ,"020202" 4fd800b3a8 2011-02-23 kinaba: ,"002020"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = {"5 3","2 4","1 5","3 2"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 16; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = 4fd800b3a8 2011-02-23 kinaba: {"00020251090265906661" 4fd800b3a8 2011-02-23 kinaba: ,"00763002550100090081" 4fd800b3a8 2011-02-23 kinaba: ,"06003699000080062771" 4fd800b3a8 2011-02-23 kinaba: ,"00000710460400035310" 4fd800b3a8 2011-02-23 kinaba: ,"50000039119198350060" 4fd800b3a8 2011-02-23 kinaba: ,"66060004050810046028" 4fd800b3a8 2011-02-23 kinaba: ,"02333108565000200880" 4fd800b3a8 2011-02-23 kinaba: ,"40212560000209205231" 4fd800b3a8 2011-02-23 kinaba: ,"02601150098329905062" 4fd800b3a8 2011-02-23 kinaba: ,"00210383709951005203" 4fd800b3a8 2011-02-23 kinaba: ,"10111087340780827070" 4fd800b3a8 2011-02-23 kinaba: ,"05065800003095040140" 4fd800b3a8 2011-02-23 kinaba: ,"15604020082000100090" 4fd800b3a8 2011-02-23 kinaba: ,"83430030070580600750" 4fd800b3a8 2011-02-23 kinaba: ,"10588355007006001150" 4fd800b3a8 2011-02-23 kinaba: ,"14400080790005400536" 4fd800b3a8 2011-02-23 kinaba: ,"23400990400933060004" 4fd800b3a8 2011-02-23 kinaba: ,"11053016300602000090" 4fd800b3a8 2011-02-23 kinaba: ,"90040920084059282502" 4fd800b3a8 2011-02-23 kinaba: ,"61300007077904050900"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = {"0 19","4 16","15 16","4 18","2 7","9 15","11 6","7 13","19 13","12 19","14 12","16 1"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 33; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = 4fd800b3a8 2011-02-23 kinaba: {"095222800320504" 4fd800b3a8 2011-02-23 kinaba: ,"107600288090501" 4fd800b3a8 2011-02-23 kinaba: ,"760973530769345" 4fd800b3a8 2011-02-23 kinaba: ,"963093337510830" 4fd800b3a8 2011-02-23 kinaba: ,"338404069255826" 4fd800b3a8 2011-02-23 kinaba: ,"291700050155264" 4fd800b3a8 2011-02-23 kinaba: ,"002783031709004" 4fd800b3a8 2011-02-23 kinaba: ,"404730701707712" 4fd800b3a8 2011-02-23 kinaba: ,"068870030090995" 4fd800b3a8 2011-02-23 kinaba: ,"320025180036103" 4fd800b3a8 2011-02-23 kinaba: ,"468695042801904" 4fd800b3a8 2011-02-23 kinaba: ,"233626561000105" 4fd800b3a8 2011-02-23 kinaba: ,"070014432197086" 4fd800b3a8 2011-02-23 kinaba: ,"887301000143802" 4fd800b3a8 2011-02-23 kinaba: ,"230852749990330"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = {"3 6","0 4","2 7","9 7","13 9","1 6","7 13","14 2","8 7","10 1","11 13","7 12"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 28; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"00401","50990","00062","08008","03000"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = {"2 4"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 14; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string roads_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: string customers_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE