7cbc6cb50e 2013-05-25 kinaba: #include <iostream> 7cbc6cb50e 2013-05-25 kinaba: #include <sstream> 7cbc6cb50e 2013-05-25 kinaba: #include <iomanip> 7cbc6cb50e 2013-05-25 kinaba: #include <vector> 7cbc6cb50e 2013-05-25 kinaba: #include <string> 7cbc6cb50e 2013-05-25 kinaba: #include <map> 7cbc6cb50e 2013-05-25 kinaba: #include <set> 7cbc6cb50e 2013-05-25 kinaba: #include <algorithm> 7cbc6cb50e 2013-05-25 kinaba: #include <numeric> 7cbc6cb50e 2013-05-25 kinaba: #include <iterator> 7cbc6cb50e 2013-05-25 kinaba: #include <functional> 7cbc6cb50e 2013-05-25 kinaba: #include <complex> 7cbc6cb50e 2013-05-25 kinaba: #include <queue> 7cbc6cb50e 2013-05-25 kinaba: #include <stack> 7cbc6cb50e 2013-05-25 kinaba: #include <cmath> 7cbc6cb50e 2013-05-25 kinaba: #include <cassert> 7cbc6cb50e 2013-05-25 kinaba: using namespace std; 7cbc6cb50e 2013-05-25 kinaba: typedef long long LL; 7cbc6cb50e 2013-05-25 kinaba: typedef long double LD; 7cbc6cb50e 2013-05-25 kinaba: typedef complex<double> CMP; 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: int bitcnt(int x) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: int c = 0; 7cbc6cb50e 2013-05-25 kinaba: for(; x; x>>=1) 7cbc6cb50e 2013-05-25 kinaba: c += x&1; 7cbc6cb50e 2013-05-25 kinaba: return c; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: class TravellingPurchasingMan { public: 7cbc6cb50e 2013-05-25 kinaba: static const int INF = 0x3fffffff; 7cbc6cb50e 2013-05-25 kinaba: struct Store { 7cbc6cb50e 2013-05-25 kinaba: int open; 7cbc6cb50e 2013-05-25 kinaba: int close; 7cbc6cb50e 2013-05-25 kinaba: int shopping; 7cbc6cb50e 2013-05-25 kinaba: }; 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: int maxStores(int N, vector <string> interestingStores, vector <string> roads) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: vector< vector<int> > d(N, vector<int>(N, INF)); 7cbc6cb50e 2013-05-25 kinaba: for(int i=0; i<N; ++i) 7cbc6cb50e 2013-05-25 kinaba: d[i][i] = 0; 7cbc6cb50e 2013-05-25 kinaba: for(int i=0; i<roads.size(); ++i) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: stringstream sin(roads[i]); 7cbc6cb50e 2013-05-25 kinaba: int a, b, len; 7cbc6cb50e 2013-05-25 kinaba: sin >> a >> b >> len; 7cbc6cb50e 2013-05-25 kinaba: d[a][b] = d[b][a] = len; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: for(int k=0; k<N; ++k) 7cbc6cb50e 2013-05-25 kinaba: for(int i=0; i<N; ++i) 7cbc6cb50e 2013-05-25 kinaba: for(int j=0; j<N; ++j) 7cbc6cb50e 2013-05-25 kinaba: d[i][j] = min(d[i][j], d[i][k]+d[k][j]); 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: const int M = interestingStores.size(); 7cbc6cb50e 2013-05-25 kinaba: vector<Store> s; 7cbc6cb50e 2013-05-25 kinaba: for(int i=0; i<interestingStores.size(); ++i) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: stringstream sin(interestingStores[i]); 7cbc6cb50e 2013-05-25 kinaba: int op,cl,du; 7cbc6cb50e 2013-05-25 kinaba: sin >> op >> cl >> du; 7cbc6cb50e 2013-05-25 kinaba: Store st = {op,cl+1,du}; // [,) 7cbc6cb50e 2013-05-25 kinaba: s.push_back(st); 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: vector< pair<int,int> > pop_mask; 7cbc6cb50e 2013-05-25 kinaba: for(int mask=0; mask<(1<<M); ++mask) 7cbc6cb50e 2013-05-25 kinaba: pop_mask.push_back(make_pair(bitcnt(mask), mask)); 7cbc6cb50e 2013-05-25 kinaba: sort(pop_mask.begin(), pop_mask.end()); 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: vector< vector<int> > tbl(M, vector<int>(1<<M, INF)); 7cbc6cb50e 2013-05-25 kinaba: for(int u=0; u<M; ++u) { 7cbc6cb50e 2013-05-25 kinaba: int t=0, v=N-1, mask=0; 7cbc6cb50e 2013-05-25 kinaba: int ta = t + d[v][u]; 7cbc6cb50e 2013-05-25 kinaba: int to = max(ta, s[u].open); 7cbc6cb50e 2013-05-25 kinaba: if(to < s[u].close) { 7cbc6cb50e 2013-05-25 kinaba: int td = to + s[u].shopping; 7cbc6cb50e 2013-05-25 kinaba: tbl[u][mask|(1<<u)] = min(tbl[u][mask|(1<<u)], td); 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: int best = 0; 7cbc6cb50e 2013-05-25 kinaba: for(int i=0; i<pop_mask.size(); ++i) { 7cbc6cb50e 2013-05-25 kinaba: int mask = pop_mask[i].second; 7cbc6cb50e 2013-05-25 kinaba: for(int v=0; v<M; ++v) if((mask&(1<<v)) && tbl[v][mask]!=INF) 7cbc6cb50e 2013-05-25 kinaba: { 7cbc6cb50e 2013-05-25 kinaba: best = pop_mask[i].first; 7cbc6cb50e 2013-05-25 kinaba: int t = tbl[v][mask]; 7cbc6cb50e 2013-05-25 kinaba: for(int u=0; u<M; ++u) if(!(mask&(1<<u))) { 7cbc6cb50e 2013-05-25 kinaba: int ta = t + d[v][u]; 7cbc6cb50e 2013-05-25 kinaba: int to = max(ta, s[u].open); 7cbc6cb50e 2013-05-25 kinaba: if(to < s[u].close) { 7cbc6cb50e 2013-05-25 kinaba: int td = to + s[u].shopping; 7cbc6cb50e 2013-05-25 kinaba: tbl[u][mask|(1<<u)] = min(tbl[u][mask|(1<<u)], td); 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: return best; 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: }; 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: // BEGIN CUT HERE 7cbc6cb50e 2013-05-25 kinaba: #include <ctime> 7cbc6cb50e 2013-05-25 kinaba: double start_time; string timer() 7cbc6cb50e 2013-05-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 7cbc6cb50e 2013-05-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 7cbc6cb50e 2013-05-25 kinaba: { os << "{ "; 7cbc6cb50e 2013-05-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 7cbc6cb50e 2013-05-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 7cbc6cb50e 2013-05-25 kinaba: void verify_case(const int& Expected, const int& Received) { 7cbc6cb50e 2013-05-25 kinaba: bool ok = (Expected == Received); 7cbc6cb50e 2013-05-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 7cbc6cb50e 2013-05-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 7cbc6cb50e 2013-05-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 7cbc6cb50e 2013-05-25 kinaba: #define END verify_case(_, TravellingPurchasingMan().maxStores(N, interestingStores, roads));} 7cbc6cb50e 2013-05-25 kinaba: int main(){ 7cbc6cb50e 2013-05-25 kinaba: 7cbc6cb50e 2013-05-25 kinaba: CASE(0) 7cbc6cb50e 2013-05-25 kinaba: int N = 3; 7cbc6cb50e 2013-05-25 kinaba: string interestingStores_[] = {"1 10 10" , "1 55 31", "10 50 100" }; 7cbc6cb50e 2013-05-25 kinaba: vector <string> interestingStores(interestingStores_, interestingStores_+sizeof(interestingStores_)/sizeof(*interestingStores_)); 7cbc6cb50e 2013-05-25 kinaba: string roads_[] = {"1 2 10"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 1; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(1) 7cbc6cb50e 2013-05-25 kinaba: int N = 3; 7cbc6cb50e 2013-05-25 kinaba: string interestingStores_[] = {"1 10 10" , "1 55 30", "10 50 100" }; 7cbc6cb50e 2013-05-25 kinaba: vector <string> interestingStores(interestingStores_, interestingStores_+sizeof(interestingStores_)/sizeof(*interestingStores_)); 7cbc6cb50e 2013-05-25 kinaba: string roads_[] = {"1 2 10"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 2; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(2) 7cbc6cb50e 2013-05-25 kinaba: int N = 5; 7cbc6cb50e 2013-05-25 kinaba: string interestingStores_[] = {"0 1000 17"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> interestingStores(interestingStores_, interestingStores_+sizeof(interestingStores_)/sizeof(*interestingStores_)); 7cbc6cb50e 2013-05-25 kinaba: string roads_[] = {"2 3 400", "4 1 500", "4 3 300", "1 0 700", "0 2 400"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 0; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: CASE(3) 7cbc6cb50e 2013-05-25 kinaba: // if no wf is done. 7cbc6cb50e 2013-05-25 kinaba: int N = 3; 7cbc6cb50e 2013-05-25 kinaba: string interestingStores_[] = {"0 100 1"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> interestingStores(interestingStores_, interestingStores_+sizeof(interestingStores_)/sizeof(*interestingStores_)); 7cbc6cb50e 2013-05-25 kinaba: string roads_[] = {"0 1 10", "1 2 10"}; 7cbc6cb50e 2013-05-25 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = 1; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: /* 7cbc6cb50e 2013-05-25 kinaba: CASE(4) 7cbc6cb50e 2013-05-25 kinaba: int N = ; 7cbc6cb50e 2013-05-25 kinaba: string interestingStores_[] = ; 7cbc6cb50e 2013-05-25 kinaba: vector <string> interestingStores(interestingStores_, interestingStores_+sizeof(interestingStores_)/sizeof(*interestingStores_)); 7cbc6cb50e 2013-05-25 kinaba: string roads_[] = ; 7cbc6cb50e 2013-05-25 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 7cbc6cb50e 2013-05-25 kinaba: int _ = ; 7cbc6cb50e 2013-05-25 kinaba: END 7cbc6cb50e 2013-05-25 kinaba: */ 7cbc6cb50e 2013-05-25 kinaba: } 7cbc6cb50e 2013-05-25 kinaba: // END CUT HERE