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 LL INF = 0x1fffffffffffffffLL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename T> 4fd800b3a8 2011-02-23 kinaba: struct DP3 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N1, N2, N3; 4fd800b3a8 2011-02-23 kinaba: vector<T> data; 4fd800b3a8 2011-02-23 kinaba: DP3(int N1, int N2, int N3, const T& t = T()) 4fd800b3a8 2011-02-23 kinaba: : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); } 4fd800b3a8 2011-02-23 kinaba: T& operator()(int i1, int i2, int i3) 4fd800b3a8 2011-02-23 kinaba: { return data[ ((i1*N2)+i2)*N3+i3 ]; } 4fd800b3a8 2011-02-23 kinaba: void swap(DP3& rhs) 4fd800b3a8 2011-02-23 kinaba: { data.swap(rhs.data); } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TimeTravellingTour { public: 4fd800b3a8 2011-02-23 kinaba: long long determineCost(int N, vector <int> cities, vector <string> roads) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > G(N, vector<LL>(N,INF)); 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string str = accumulate(roads.begin(), roads.end(), string("")); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<str.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( str[i] == ',' ) 4fd800b3a8 2011-02-23 kinaba: str[i] = ' '; 4fd800b3a8 2011-02-23 kinaba: stringstream sin(str); 4fd800b3a8 2011-02-23 kinaba: for(int a,b,cost; sin>>a>>b>>cost; ) 4fd800b3a8 2011-02-23 kinaba: G[a][b] = G[b][a] = cost; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: G[i][i] = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<N; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: G[i][j] = min(G[i][j], G[i][k]+G[k][j]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: DP3<LL> memo(N, cities.size()+1, cities.size()+1, -1); 4fd800b3a8 2011-02-23 kinaba: LL a = rec( 0, 0, cities.size(), cities, G, memo ); 4fd800b3a8 2011-02-23 kinaba: return a>=INF ? -1 : a; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL rec(int cur, int s, int e, const vector<int>& C, const vector< vector<LL> >& G, DP3<LL>& memo ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( s+1 == e ) 4fd800b3a8 2011-02-23 kinaba: return G[cur][C[s]]; 4fd800b3a8 2011-02-23 kinaba: if( memo(cur,s,e) >= 0 ) 4fd800b3a8 2011-02-23 kinaba: return memo(cur,s,e); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL best = INF; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<G[cur].size(); ++v) 4fd800b3a8 2011-02-23 kinaba: if( G[cur][v] < INF ) 4fd800b3a8 2011-02-23 kinaba: for(int m=s+1; m<e; ++m) 4fd800b3a8 2011-02-23 kinaba: best = min(best, G[cur][v]+rec(v,s,m,C,G,memo)+rec(v,m,e,C,G,memo)); 4fd800b3a8 2011-02-23 kinaba: return memo(cur,s,e) = 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 long long& Expected, const long long& 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(_, TimeTravellingTour().determineCost(N, cities, roads));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = {2,3,2,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"0,2,4 0,1,2 2,1,2 1,3,3 4,0,4"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: long long _ = 13LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = {1,0,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"0,2,1"," 2",",1,5"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: long long _ = 12LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int N = 3; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"0,1,2"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: long long _ = -1LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int N = 6; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = {4, 1, 3, 2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"0,1,5 0,2,5 0,5,2 2,3,5 2,4,2 3,4,4 3,5,1"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: long long _ = 19LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int N = ; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 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: long long _ = LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int N = ; 4fd800b3a8 2011-02-23 kinaba: int cities_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)); 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: long long _ = LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE