Appoint description:
Description
Input
Input contains several cases.
Each cases begins with an integer n, 1 n 100, 000, to indicate the size of the point set. The points in the point set have serial numbers from 1 to n.
Each line of the following n lines contains two non-negative integers (xi, yi) (no more than 107) to describe the coordinate for each point. Any two points' coordinates are different.
The last case is followed by a line containing a zero.
Output
For each case, output the case's serial number and the weighted sum of all minimum spanning tree edges in the following format.
Sample Input
3 0 0 2 0 0 3 0
Sample Output
Case 1: Total Weight = 5
给定n个点,计算最小生成树边权和,采用曼哈顿最小生成树,可以得到。
代码:
/* ***********************************************
Author :rabbit
Created Time :2014/3/12 13:48:39
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
struct Point{
int x,y,id;
bool operator < (const Point p) const {
if(x!=p.x)return x<p.x;
return y<p.y;
}
}p[maxn];
struct BIT{
int min_val,pos;
void init(){
min_val=INF;
pos=-1;
}
}bit[maxn<<2];
struct Edge{
int u,v,d;
bool operator < (const Edge p) const {
return d<p.d;
}
}edge[maxn<<2];
int tot,n,F[maxn];
int find(int x){
return F[x]=(F[x]==x?x:find(F[x]));
}
void addedge(int u,int v,int d){
edge[tot].u=u;edge[tot].v=v;edge[tot].d=d;tot++;
}
void update(int i,int val,int pos){
while(i>0){
if(val<bit[i].min_val){
bit[i].min_val=val;
bit[i].pos=pos;
}
i-=i&(-i);
}
}
int ask(int i,int m){
int min_val=INF,pos=-1;
while(i<=m){
if(bit[i].min_val<min_val){
min_val=bit[i].min_val;
pos=bit[i].pos;
}
i+=i&(-i);
}
return pos;
}
int dist(Point a,Point b){
return abs(a.y-b.y)+abs(a.x-b.x);
}
ll MHT(int n,Point *p){
int a[maxn],b[maxn];
tot=0;ll ans=0;
for(int dir=0;dir<4;dir++){
if(dir==1||dir==3){
for(int i=0;i<n;i++)
swap(p[i].x,p[i].y);
}
if(dir==2){
for(int i=0;i<n;i++)
p[i].x=-p[i].x;
}
sort(p,p+n);
for(int i=0;i<n;i++)
a[i]=b[i]=p[i].y-p[i].x;
sort(b,b+n);
int m=unique(b,b+n)-b;
for(int i=1;i<=m;i++)bit[i].init();
for(int i=n-1;i>=0;i--){
int pos=lower_bound(b,b+m,a[i])-b+1;
int ans=ask(pos,m);
if(ans!=-1)addedge(p[i].id,p[ans].id,dist(p[i],p[ans]));
update(pos,p[i].x+p[i].y,i);
}
}
sort(edge,edge+tot);
for(int i=0;i<n;i++)F[i]=i;
for(int i=0;i<tot;i++){
int u=edge[i].u,v=edge[i].v;
int fa=find(u),fb=find(v);
if(fa!=fb){
ans+=edge[i].d;
F[fa]=fb;
}
}
return ans;
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n,ncase=0;
while(~scanf("%d",&n)&&n){
for(int i=0;i<n;i++)scanf("%d%d",&p[i].x,&p[i].y),p[i].id=i;
printf("Case %d: Total Weight = %lld\n",++ncase,MHT(n,p));
}
return 0;
}