Advertisement
iskhakovt

Untitled

Mar 26th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.71 KB | None | 0 0
  1. #include "testlib.h"
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. double EPS = 1e-6;
  7.  
  8. template <typename ValueType>
  9. ValueType sqr(ValueType a) {
  10.     return a * a;
  11. }
  12.  
  13. template <typename ValueType>
  14. class Point {
  15.     ValueType x, y;
  16.  public:
  17.  
  18.     explicit Point()
  19.     {}
  20.  
  21.     explicit Point(const ValueType &x, const ValueType &y):
  22.         x(x), y(y)
  23.     {}
  24.  
  25.     friend std::istream &operator >> (std::istream &is, Point &point) {
  26.         return is >> point.x >> point.y;
  27.     }
  28.  
  29.     friend std::ostream &operator << (std::ostream &os, const Point &point) {
  30.         return os << point.x << ' ' << point.y;
  31.     }
  32.  
  33.     ValueType X() const {
  34.         return x;
  35.     }
  36.  
  37.     ValueType Y() const {
  38.         return y;
  39.     }
  40.  
  41.     bool operator < (const Point &other) const {
  42.         return x < other.x || (x == other.x && y < other.y);
  43.     }
  44.  
  45.     bool operator == (const Point &other) const {
  46.         return x == other.X() && y == other.Y();
  47.     }
  48.  
  49.     double Distance(const Point &other) const {
  50.         return sqrt(sqr(x - other.X()) + sqr(y - other.Y()));
  51.     }
  52. };
  53.  
  54. template <typename ValueType>
  55. class PointAndGraph {
  56.     vector <pair<size_t, size_t> > graph;
  57.     vector <Point<ValueType> > point;
  58.     double price;
  59.     double MAX_DISTANCE;
  60.  public:
  61.  
  62.     explicit PointAndGraph()
  63.     {}
  64.  
  65.     explicit PointAndGraph(const vector<Point<ValueType> > &point):
  66.         point(point)
  67.     {
  68.         vector<vector<double> > matrix = GetAdjacencyMatrix();
  69.         Prima(matrix);
  70.     }
  71.  
  72.     vector<vector<double> > GetAdjacencyMatrix() {
  73.         MAX_DISTANCE = 0;
  74.         vector<vector<double> > result(point.size(), vector<double> (point.size(), 0));
  75.         for (size_t i = 0; i < point.size(); ++i) {
  76.             for (size_t j = 0; j < point.size(); ++j) {
  77.                 result[i][j] = point[i].Distance(point[j]);
  78.                 if (MAX_DISTANCE < result[i][j]) {
  79.                     MAX_DISTANCE = result[i][j];
  80.                 }
  81.             }
  82.             MAX_DISTANCE *= 2;
  83.         }
  84.         return result;
  85.     }
  86.  
  87.     void Prima(const vector<vector<double> > &matrix) {
  88.         size_t size = matrix.size(), start = 0;
  89.         vector<char> used(size);
  90.         vector <double> distance(size, MAX_DISTANCE);
  91.         vector<size_t> parent(size);
  92.         distance[start] = 0;
  93.         price = 0;
  94.         for (size_t q = 0; q < size; ++q) {
  95.             size_t index_minimum = size;
  96.             for (size_t index = 0; index < size; ++index) {
  97.                 if (!used[index] && (index_minimum == size || distance[index] < distance[index_minimum])) {
  98.                     index_minimum = index;
  99.                 }
  100.             }
  101.             if (index_minimum == size) {
  102.                 continue;
  103.             }
  104.             used[index_minimum] = 1;
  105.             if (index_minimum != start) {
  106.                 graph.push_back({index_minimum, parent[index_minimum]});
  107.                 price += distance[index_minimum];
  108.             }
  109.             for (size_t index = 0; index < size; ++index) {
  110.                 if (!used[index] && distance[index] - EPS > matrix[index_minimum][index]) {
  111.                     distance[index] = matrix[index_minimum][index];
  112.                     parent[index] = index_minimum;
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     vector <pair<size_t, size_t> > GetGraph() const {
  119.         return graph;
  120.     }
  121.  
  122.     vector <Point<ValueType> > GetPoint() const {
  123.         return point;
  124.     }
  125.  
  126.     double GetPrice() const {
  127.         return price;
  128.     }
  129.  
  130.     bool operator < (const PointAndGraph &other) const {
  131.         return price < other.price;
  132.     }
  133.  
  134. };
  135.  
  136.  
  137.  
  138. void ReadVectorPoint(InStream &is, vector<Point<int64_t> >::iterator begin,
  139.     vector<Point<int64_t> >::iterator end, int64_t border, set<Point<int64_t> > &set_point, TResult _pe) {
  140.     for (; begin != end; ++begin) {
  141.         int64_t x = is.readInt(-border, border);
  142.         int64_t y = is.readInt(-border, border);
  143.         (*begin) = Point<int64_t> (x, y);
  144.         if (set_point.count(*begin)) {
  145.             quitf(_pe, "there equal point");
  146.         }
  147.         set_point.insert(*begin);
  148.     }
  149. }
  150.  
  151. double Solve(InStream &is, const vector<Point<int64_t> > &basic, int64_t border, int64_t border_extra_size,
  152.     set<Point<int64_t> > set_point, TResult _pe){
  153.     int extra_size = is.readInt(0, border_extra_size);
  154.     vector<Point<int64_t> > extra_points(extra_size);
  155.     ReadVectorPoint(is, extra_points.begin(), extra_points.end(), border, set_point, _pe);
  156.     double answer = (PointAndGraph<int64_t>(basic)).GetPrice();
  157.  
  158.     for (int subset = 1; subset != (1 << extra_size); ++subset) {
  159.         vector<Point<int64_t> > points = basic;
  160.         for (int i = 0; i != extra_size; ++i) {
  161.             if (subset & (1 << i)) {
  162.                 points.push_back(extra_points[i]);
  163.             }
  164.         }
  165.         answer = std::min(answer, (PointAndGraph<int64_t>(points)).GetPrice());
  166.     }
  167.  
  168.     return answer;
  169. }
  170.  
  171. int main(int argc, char **argv)
  172. {
  173.     registerTestlibCmd(argc, argv);
  174.     int basic_size = inf.readInt(), extra_size = inf.readInt();
  175.     if (basic_size < 0 || extra_size < 0) {
  176.        quitf(_fail, "input basic_size = %d, extra_size = %d", basic_size, extra_size);
  177.     }
  178.     vector<Point<int64_t> > basic(basic_size);
  179.     int64_t border = 1e4;
  180.     set<Point<int64_t> > set_point;
  181.     ReadVectorPoint(inf, basic.begin(), basic.end(), border, set_point, _fail);
  182.     double clean_answer = PointAndGraph<int64_t> (basic).GetPrice();
  183.     double best_answer = Solve(ans, basic, border, extra_size, set_point, _fail);
  184.     double user_answer = Solve(ouf, basic, border, extra_size, set_point,  _pe);
  185.     quitp(max((double)0, floor(70.0 * (clean_answer - user_answer) / (clean_answer - best_answer))), "");
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement