POJ 3241 Object Clustering 2D plane Manhattan distance min Spanning Tree

Source: Internet
Author: User

POJ 3241 Object Clustering 2D plane Manhattan distance min Spanning Tree

 

Question:

Given the coordinates of n points on a two-dimensional plane, the constant k

The following n lines provide coordinates.

Find the smallest Spanning Tree and ask the number of edges at k.

The cost of building an edge between any two points is its Manhattan distance.

 

 

I. Minimum Spanning Tree distance from Manhattan

The problem of the Minimum Spanning Tree of Manhattan distance can be described as follows:

Given the N points on a two-dimensional plane, the cost of EDGE connection between two points is its Manhattan distance, and the minimum cost of connecting all points.

A simple algorithm can use the Prim of O (N2) or process all edges as Kruskal. But here there are O (N2) Total edges, therefore, the complexity of Kruskal is changed to O (N2logN ).

But in fact, there are no remote O (N2) lines that are actually useful. We will consider what kind of points will be connected to each other. We can draw a conclusion that a Cartesian coordinate system is built with a point as the origin, and only the edge is connected to the point closest to the point at every 45 degrees.

This conclusion can be proved as follows: assume that we establish A system with point A as the origin, consider any two points B (x1, y1) and C (x2, y2) in the right 45 degrees area of the Y axis ), | AB | ≤| AC | (the distance here is the distance from Manhattan), for example:

 

| AB | = x1 + y1, | AC | = x2 + y2, | BC | = | x1-x2 | + | y1-y2 |. Because both B and C are in the right 45 degrees area of the y axis, there are y-x> 0 and x> 0. We will discuss the following situation:

1. x1> x2 and y1> y2. This conflicts with | AB | ≤| AC |;

2. x1 ≤ x2 and y1> y2. At this time | BC | = x2-x1 + y1-y2, | AC |-| BC | = x2 + y2-x2 + x1-y1 + y2 = x1-y1 + 2 * y2. The preceding relationships can be y1> y2> x2> x1. Suppose | AC | <| BC | that is, y1> 2 * y2 + x1, then | AB | = x1 + y1> 2 * x1 + 2 * y2, | AC | = x2 + y2 <2 * y2 <| AB | conflict with the premise, hence | AC | ≥| BC |;

3. x1> x2 and y1 ≤ y2. Same as 2;

4. x1 ≤ x2 and y1 ≤ y2. In this case, it is clear that | AB | + | BC | = | AC |, that is, | AC |> | BC |.

In summary, we only need to select the vertex closest to A to the side in this region. | AC | ≥| BC |.

This EDGE connection method ensures that the number of edges is O (N). If you can efficiently process these edges, you can use Kruskal to solve the problem in O (NlogN) time. Next we will consider how to efficiently process edges.

We only need to consider the points in one area. The points in other areas can be moved to this area through coordinate transformation. For ease of processing, we consider the right 45 degrees area on the Y axis. Point B (x1, y1) within this region of A point A (x0, y0) satisfies x1 ≥ x0 and y1-x1> y0-x0. Here we only take one side of the boundary, but it doesn't matter if we take both sides of the operation. So | AB | = y1-y0 + x1-x0 = (x1 + y1)-(x0 + y0 ). The point closest to A in the area of A is the point with the minimum x + y in the condition. Therefore, we can sort all vertices by x coordinates, and then discrete by y-x, use the line segment tree or tree array to maintain the points corresponding to the smallest x + y value greater than the y-x value of the current point. Time complexity O (NlogN ).

As for coordinate transformation, a better method is to do it directly for the first time; the second flip along the line y = x, that is, swap the x and y coordinates; the third flip along the line x = 0, that is, take the opposite number of x coordinates, and flip along the line y = x for the fourth time. Note that only four operations are required, because the edges are bidirectional.

So far, the entire problem can be solved within the complexity of O (NlogN.


 

#include 
 
  #include 
  
   #include #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include 
             
              #include 
              
               #include
               const int inf = 1e8;const double eps = 1e-8;const double pi = acos(-1.0);template 
                
                 inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template 
                 
                  inline void pt(T x) {if (x <0) { putchar('-'); x = -x; }if (x>9) pt(x / 10);putchar(x % 10 + '0');}using namespace std;const int N = 1e5 + 10;class MST{struct Edge{int from, to, dis;Edge(int _from = 0, int _to = 0, int _dis = 0) :from(_from), to(_to), dis(_dis){}bool operator < (const Edge &x) const{return dis < x.dis;}}edge[N << 3];int f[N], tot;int find(int x){ return x == f[x] ? x : f[x] = find(f[x]); }bool Union(int x, int y){x = find(x); y = find(y);if (x == y)return false;if (x > y)swap(x, y);f[x] = y;return true;}public:void init(int n){for (int i = 0; i <= n; i++)f[i] = i;tot = 0;}void add(int u, int v, int dis){edge[tot++] = Edge(u, v, dis);}int work(){sort(edge, edge + tot);int cost = 0;for (int i = 0; i < tot; i++){if (Union(edge[i].from, edge[i].to))cost += edge[i].dis;}return cost;}int work_kth(int k){sort(edge, edge + tot);int cost = 0;for (int i = 0; i < tot && k; i++){if (Union(edge[i].from, edge[i].to))cost = edge[i].dis, k--;}return cost;}}mst;struct Point{int x, y, id;friend bool operator<(const Point&a, const Point&b){if (a.x == b.x)return a.y < b.y;return a.x < b.x;}}p[N];class BIT{int c[N], id[N], maxn;int lowbit(int x){ return x&-x; }public:void init(int n){maxn = n + 10;fill(c, c + maxn + 1, inf);fill(id, id + maxn + 1, -1);}void updata(int pos, int val, int _id){while (pos){if (c[pos] > val){ c[pos] = val; id[pos] = _id; }pos -= lowbit(pos);}}int query(int pos){int val = inf, _id = -1;while (pos <= maxn){if (val > c[pos]){ val = c[pos]; _id = id[pos]; }pos += lowbit(pos);}return _id;}}tree;inline bool cmp(int *x, int *y){ return *x < *y; }class Manhattan_MST{int *po[N], a[N]; public:int work(int l, int r, int k){mst.init(r);for (int dir = 1; dir <= 4; dir++){if (dir%2==0)for (int i = l; i <= r; i++)swap(p[i].x, p[i].y);else if (dir == 3)for (int i = l; i <= r; i++)p[i].x = -p[i].x;sort(p + l, p + r + 1);for (int i = l; i <= r; ++i) a[i] = p[i].y - p[i].x, po[i] = &a[i];sort(po + l, po + r + 1, cmp);for (int i = l; i <= r; i++)*po[i] = i;tree.init(r);for (int i = r; i >= l; i--){int id = tree.query(a[i]);if (id != -1)mst.add(p[i].id, p[id].id, abs(p[i].x - p[id].x) + abs(p[i].y - p[id].y));tree.updata(a[i], p[i].x + p[i].y, i);}}return mst.work_kth(k);}}m_mst;int n, k;int main(){rd(n); rd(k);for (int i = 1; i <= n; i++)rd(p[i].x), rd(p[i].y), p[i].id = i;pt(m_mst.work(1, n, n-k)); puts("");return 0;}
                 
                
              
             
            
           
          
         
        
       
      
     
    
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.