Artifact Content
Not logged in

Artifact 800766c7be5a99d9127df75a1faf62c1c4b24abc


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class BuildingCities { public:
   int findMinimumCities(int maxDirect, int maxTravel, vector <int> cityX, vector <int> cityY) 
   {
      int result = 0x7fffffff;

      const int N = cityX.size();
      typedef pair<int,    int>  vert; // city, extra_city_built
      typedef pair<double, vert> cedge;

      // dijkstra
      set<vert> V;
      priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
      Q.push( cedge(0, vert(0,0)) );
      while( !Q.empty() )
      {
         double d = Q.top().first;
         vert   v = Q.top().second;
         Q.pop();
         if( V.count(v) )
            continue;
         V.insert(v);

         int vc = v.first;
         int vs = v.second;
         if( vc==N-1 )
            result = min(result, vs);

         for(int j=0; j<N; ++j) if( vc != j )
         {
            double dd = hypot( cityX[vc] -cityX[j], cityY[vc] -cityY[j] );
            double dx = hypot( cityX[N-1]-cityX[j], cityY[N-1]-cityY[j] );
            vert u(j, vs+int(dd/maxDirect - 1e-10));
            if( d+dd+dx<=maxTravel && !V.count(u) && u.second<result )
               Q.push( cedge(d+dd, u) );
         }
      }
      return result==0x7fffffff ? -1 : result;
   }
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END    verify_case(_, BuildingCities().findMinimumCities(maxDirect, maxTravel, cityX, cityY));}
int main(){

CASE(0)
   int maxDirect = 1; 
   int maxTravel = 5; 
   int cityX_[] = {0,0,0,1,2,2,3};
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = {0,1,2,2,2,1,1};
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = 1; 
END
CASE(1)
   int maxDirect = 3; 
   int maxTravel = 15; 
   int cityX_[] = {2,6,10,14};
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = {2,6,2,6};
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = 3; 
END
CASE(2)
   int maxDirect = 2; 
   int maxTravel = 15; 
   int cityX_[] = {0,5,2,3,1,8,6,4,7,9,10};
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = {0,5,2,3,1,8,6,4,7,9,10};
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = 0; 
END
CASE(3)
   int maxDirect = 2; 
   int maxTravel = 5; 
   int cityX_[] = {0,5};
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = {0,5};
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = -1; 
END
CASE(4)
   int maxDirect = 5; 
   int maxTravel = 1500; 
   int cityX_[] = {0,1000};
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = {0,1000};
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = 282; 
END
/*
CASE(5)
   int maxDirect = ; 
   int maxTravel = ; 
   int cityX_[] = ;
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = ;
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = ; 
END
CASE(6)
   int maxDirect = ; 
   int maxTravel = ; 
   int cityX_[] = ;
     vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
   int cityY_[] = ;
     vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
   int _ = ; 
END
*/
}
// END CUT HERE