File Annotation
Not logged in
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: class BuildingCities { public:
4fd800b3a8 2011-02-23        kinaba:    int findMinimumCities(int maxDirect, int maxTravel, vector <int> cityX, vector <int> cityY)
4fd800b3a8 2011-02-23        kinaba:    {
4fd800b3a8 2011-02-23        kinaba:       int result = 0x7fffffff;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba:       const int N = cityX.size();
4fd800b3a8 2011-02-23        kinaba:       typedef pair<int,    int>  vert; // city, extra_city_built
4fd800b3a8 2011-02-23        kinaba:       typedef pair<double, vert> cedge;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba:       // dijkstra
4fd800b3a8 2011-02-23        kinaba:       set<vert> V;
4fd800b3a8 2011-02-23        kinaba:       priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
4fd800b3a8 2011-02-23        kinaba:       Q.push( cedge(0, vert(0,0)) );
4fd800b3a8 2011-02-23        kinaba:       while( !Q.empty() )
4fd800b3a8 2011-02-23        kinaba:       {
4fd800b3a8 2011-02-23        kinaba:          double d = Q.top().first;
4fd800b3a8 2011-02-23        kinaba:          vert   v = Q.top().second;
4fd800b3a8 2011-02-23        kinaba:          Q.pop();
4fd800b3a8 2011-02-23        kinaba:          if( V.count(v) )
4fd800b3a8 2011-02-23        kinaba:             continue;
4fd800b3a8 2011-02-23        kinaba:          V.insert(v);
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba:          int vc = v.first;
4fd800b3a8 2011-02-23        kinaba:          int vs = v.second;
4fd800b3a8 2011-02-23        kinaba:          if( vc==N-1 )
4fd800b3a8 2011-02-23        kinaba:             result = min(result, vs);
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba:          for(int j=0; j<N; ++j) if( vc != j )
4fd800b3a8 2011-02-23        kinaba:          {
4fd800b3a8 2011-02-23        kinaba:             double dd = hypot( cityX[vc] -cityX[j], cityY[vc] -cityY[j] );
4fd800b3a8 2011-02-23        kinaba:             double dx = hypot( cityX[N-1]-cityX[j], cityY[N-1]-cityY[j] );
4fd800b3a8 2011-02-23        kinaba:             vert u(j, vs+int(dd/maxDirect - 1e-10));
4fd800b3a8 2011-02-23        kinaba:             if( d+dd+dx<=maxTravel && !V.count(u) && u.second<result )
4fd800b3a8 2011-02-23        kinaba:                Q.push( cedge(d+dd, u) );
4fd800b3a8 2011-02-23        kinaba:          }
4fd800b3a8 2011-02-23        kinaba:       }
4fd800b3a8 2011-02-23        kinaba:       return result==0x7fffffff ? -1 : result;
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(_, BuildingCities().findMinimumCities(maxDirect, maxTravel, cityX, cityY));}
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 maxDirect = 1;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = 5;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = {0,0,0,1,2,2,3};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = {0,1,2,2,2,1,1};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = 1;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = 3;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = 15;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = {2,6,10,14};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = {2,6,2,6};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = 3;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = 2;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = 15;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = {0,5,2,3,1,8,6,4,7,9,10};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = {0,5,2,3,1,8,6,4,7,9,10};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = 0;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = 2;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = 5;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = {0,5};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = {0,5};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = -1;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(4)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = 5;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = 1500;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = {0,1000};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = {0,1000};
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = 282;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: /*
4fd800b3a8 2011-02-23        kinaba: CASE(5)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = ;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = ;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = ;
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = ;
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
4fd800b3a8 2011-02-23        kinaba:    int _ = ;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(6)
4fd800b3a8 2011-02-23        kinaba:    int maxDirect = ;
4fd800b3a8 2011-02-23        kinaba:    int maxTravel = ;
4fd800b3a8 2011-02-23        kinaba:    int cityX_[] = ;
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_));
4fd800b3a8 2011-02-23        kinaba:    int cityY_[] = ;
4fd800b3a8 2011-02-23        kinaba:      vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_));
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