POJ - 3241 Object Clustering 哈夫曼距离最小生成树
2017-06-13 18:06
393 查看
题目链接点这里
最近真是很怠情啊,不想动手。。。
偶尔敲一题,,都是敲一个小时,,调试2个小时的状态,,,,心累,,
#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"x"<<endl;
#define MX 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-5;
int n,k;
const int M=2222,U=1000;//M为U-(y-x)取到的最大值;
struct Point {
int x,y,id;
bool operator <(const Point a)const {
if(x!=a.x)return x<a.x;
return y-x<a.y-a.x;
}
} w[MX];
struct Edge {
int u,v,dist;
bool operator <(const Edge a)const {
return dist<a.dist;
}
Edge() {}
Edge(int u,int v,int dist):u(u),v(v),dist(dist) {}
} E[10*MX];
int cnt;
struct Tree {
int val,id;
} tree[MX];
void tree_add(int x,int val,int id) {
while(x<=M) {
if(tree[x].val>val) {
tree[x].val=val;
tree[x].id=id;
}
x+=-x&x;
}
}
PII tree_query(int x) {
int minn=INF,id=-1;
while(x) {
if(tree[x].val<minn) {
minn=tree[x].val;
id=tree[x].id;
}
x-=-x&x;
}
return PII(minn,id);
}
void build() {
for(int i=0; i<=M; i++)tree[i].id=-1,tree[i].val=INF;
sort(w,w+n);
for(int i=n-1; i>=0; i--) {
PII u=tree_query(U-(w[i].y-w[i].x));
if(u.second!=-1)
E[cnt++]=Edge(w[i].id,u.second,u.first-(w[i].x+w[i].y));
tree_add(U-(w[i].y-w[i].x),w[i].y+w[i].x,w[i].id);
}
}
int bin[MX];
int Find(int x) {
return x==bin[x]?x:bin[x]=Find(bin[x]);
}
int main() {
FIN;
while(cin>>n>>k) {
for(int i=0; i<n; i++) {
scanf("%d%d",&w[i].x,&w[i].y);
w[i].id=i;
}
cnt=0;
build();
//以x=y翻转
for(int i=0; i<n; i++)swap(w[i].x,w[i].y);
build();
//以x=0翻转
for(int i=0; i<n; i++)w[i].x=-w[i].x;
build();
//以x=y翻转
for(int i=0; i<n; i++)swap(w[i].x,w[i].y);
build();
//求最小生成树部分
sort(E,E+cnt);//有重边
for(int i=1; i<=n; i++)bin[i]=i;
int t=0,ans=0;
k=n-1-k+1;
for(int i=0; i<cnt; i++) {
int fa=Find(E[i].u),fb=Find(E[i].v);
if(fa!=fb)t++,bin[fa]=fb;
if(t==k) {
ans=E[i].dist;
break;
}
}
cout<<ans<<endl;
}
return 0;
}
最近真是很怠情啊,不想动手。。。
偶尔敲一题,,都是敲一个小时,,调试2个小时的状态,,,,心累,,
#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"x"<<endl;
#define MX 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-5;
int n,k;
const int M=2222,U=1000;//M为U-(y-x)取到的最大值;
struct Point {
int x,y,id;
bool operator <(const Point a)const {
if(x!=a.x)return x<a.x;
return y-x<a.y-a.x;
}
} w[MX];
struct Edge {
int u,v,dist;
bool operator <(const Edge a)const {
return dist<a.dist;
}
Edge() {}
Edge(int u,int v,int dist):u(u),v(v),dist(dist) {}
} E[10*MX];
int cnt;
struct Tree {
int val,id;
} tree[MX];
void tree_add(int x,int val,int id) {
while(x<=M) {
if(tree[x].val>val) {
tree[x].val=val;
tree[x].id=id;
}
x+=-x&x;
}
}
PII tree_query(int x) {
int minn=INF,id=-1;
while(x) {
if(tree[x].val<minn) {
minn=tree[x].val;
id=tree[x].id;
}
x-=-x&x;
}
return PII(minn,id);
}
void build() {
for(int i=0; i<=M; i++)tree[i].id=-1,tree[i].val=INF;
sort(w,w+n);
for(int i=n-1; i>=0; i--) {
PII u=tree_query(U-(w[i].y-w[i].x));
if(u.second!=-1)
E[cnt++]=Edge(w[i].id,u.second,u.first-(w[i].x+w[i].y));
tree_add(U-(w[i].y-w[i].x),w[i].y+w[i].x,w[i].id);
}
}
int bin[MX];
int Find(int x) {
return x==bin[x]?x:bin[x]=Find(bin[x]);
}
int main() {
FIN;
while(cin>>n>>k) {
for(int i=0; i<n; i++) {
scanf("%d%d",&w[i].x,&w[i].y);
w[i].id=i;
}
cnt=0;
build();
//以x=y翻转
for(int i=0; i<n; i++)swap(w[i].x,w[i].y);
build();
//以x=0翻转
for(int i=0; i<n; i++)w[i].x=-w[i].x;
build();
//以x=y翻转
for(int i=0; i<n; i++)swap(w[i].x,w[i].y);
build();
//求最小生成树部分
sort(E,E+cnt);//有重边
for(int i=1; i<=n; i++)bin[i]=i;
int t=0,ans=0;
k=n-1-k+1;
for(int i=0; i<cnt; i++) {
int fa=Find(E[i].u),fb=Find(E[i].v);
if(fa!=fb)t++,bin[fa]=fb;
if(t==k) {
ans=E[i].dist;
break;
}
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- POJ 3241 Object Clustering 曼哈顿距离求最小生成树的第K小边
- poj 3241 Object Clustering (曼哈顿距离最小生成树)
- [曼哈顿距离最小生成树 模板题] POJ 3241 Object Clustering
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
- POJ 3214 Object Clustering 哈夫曼距离最小生成树
- POJ 3241 Object Clustering 二维平面曼哈顿距离最小生成树
- POJ 3241 Object Clustering 曼哈顿距离最小生成树
- POJ 3241 Object Clustering (曼哈顿距离最小生成树)
- poj 3241 Object Clustering(曼哈顿最小生成树)
- POJ 3241 Object Clustering 曼哈顿最小生成树
- POJ-3241 Object Clustering 曼哈顿最小生成树
- Object Clustering - POJ 3241 曼哈顿最小生成树
- POJ 3241 Object Clustering 平面曼哈顿最小生成树
- 平面点曼哈顿最小生成树——POJ 3241 Object Clustering
- poj 3241 Object Clustering 曼哈顿最小生成树
- POJ - 3241 Object Clustering(莫队算法/曼哈顿最小生成树)
- poj 3241 Object Clustering 莫队算法,曼哈顿最小生成树
- poj 3241 Object Clustering 曼哈顿最小生成树
- POJ 3241 曼哈顿距离最小生成树 Object Clustering
- POJ 3241 (曼哈顿距离最小生成树)