2018東北四省賽 Spin A Web 曼哈頓距離最小生成樹
問題描述:給定平面N個點,兩邊相連的代價為曼哈頓距離,求這些點的最小生成樹
按一般想法,prime複雜度O(n^2),Kruskal複雜度O(n^2 logn),N很大時,這複雜度要爆炸了
但是最小生成樹具有一個性質——環切性質,即如果在一個圖中存在一個環,把環中權最大的邊刪去,那麼現在最小生成樹的權和
刪之前相同,所以很多邊都是沒用的,可以刪去
在平面內,分割成八個區域
可以證明,中心的原點只需和每個區域的一個點相連即可(證明當然是要看莫隊的)
所以構造的圖中至多有8n條邊,複雜度為O(nlogn)
以區域1為例,設原點為O(x0,y0),另一點P(x1,y1),則 x1>x0 且 y1-x1 > y0-y1,滿足x1+y1最小的點即是最近的點
實現使先將x排序,再將y-x排序,然後用樹狀陣列維護x+y的最小值
然後八個區域轉換後可以到區域1,因為是雙向邊,所以四次轉換就可以
第一次直接計算,第二次按y=x翻轉,即x與y座標互換,第三次按x=0翻轉,即x座標為負數,第四次在按y=x翻轉,就可以計算了
7231: Spin A Web
時間限制: 1 Sec 記憶體限制: 128 MB
提交: 127 解決: 24
[提交] [狀態] [討論版] [命題人:admin]
題目描述
We have a canvas divided into grid with H rows and W columns. The square at the ith row from the top and the jth column from the left is represented as (i, j). (i, j) square has two value xi,j and yi,j .
Now we want to merge the squares to a connected web with minimal cost. Two squares can be connected if they are in the same row or column, and the cost of connecting (i0, j0) and (i1, j1) is
|xi0,j0 − xi1,j1 | + |yi0,j0 − yi1,j1 |.
輸入
Input is given from Standard Input in the following format:
H W
x1,1 x1,2 . . . x1,W
.
.
xH,1 xH,2 . . . xH,W
y1,1 y1,2 . . . y1,W
.
.
yH,1 yH,2 . . . yH,W
Constraints
1 ≤ H × W ≤ 100000
−108 ≤ xi,j, yi,j ≤ 108(1 ≤ i ≤ H, 1 ≤ j ≤ W )
All of them are integers.
輸出
Print one line denotes the minimal cost to merge the square to be a connected web.
樣例輸入
1 3 1 3 2 1 2 3
樣例輸出
5
這個題只能同行或者同列相連,所以對每行每列求一次
程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
const int inf = 0x3f3f3f3f;
inline int read() {
char c;
int sum = 0;
int f = 1;
c = getchar();
while (c < '0' || c > '9') {
if (c == '-')f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
return sum * f;
}
struct node {
int x, y, pos;
friend bool operator<(node a, node b) {
if (a.x != b.x)return a.x < b.x;
return a.y < b.y;
}
} s[maxn];
int p[maxn];
struct node2 {
int u, v, val;
friend bool operator<(node2 x, node2 y) {
return x.val < y.val;
}
} edg[maxn * 8];
int tot = 0;
int a[maxn], b[maxn];
bool cmp(int a, int b) {
if (s[a].x != s[b].x) return s[a].x < s[b].x;
return s[a].y < s[b].y;
}
bool cmp2(node2 x, node2 y) {
return x.val < y.val;
}
int value[maxn], poss[maxn];
int fa[maxn];
void add(int x, int val, int pos) {
for (int i = x; i > 0; i -= (i & (-i))) {
if (value[i] > val) {
value[i] = val;
poss[i] = pos;
}
}
}
int ask(int x, int n) {
int ma = inf;
int ans = -1;
for (int i = x; i <= n; i += (i & (-i))) {
if (value[i] < ma) {
ma = value[i];
ans = poss[i];
}
}
return ans;
}
int findd(int x) {
return fa[x] == x ? x : fa[x] = findd(fa[x]);
}
void Union(int x, int y) {
int fx = findd(x);
int fy = findd(y);
if (fx != fy) {
fa[fx] = fy;
}
}
void mandis(int n) {
sort(p, p + n, cmp);
for (int i = 0; i < n; i++) {
a[i] = b[i] = s[p[i]].y - s[p[i]].x;
}
sort(b, b + n);
int k = unique(b, b + n) - b;
for (int i = 1; i <= n; i++) {
value[i] = inf;
poss[i] = -1;
}
for (int i = n - 1; i >= 0; i--) {
int pp = lower_bound(b, b + k, a[i]) - b + 1;
int ans = ask(pp, n);
if (ans != -1) {
edg[tot].u = s[p[i]].pos;
edg[tot].v = s[p[ans]].pos;
edg[tot++].val = abs(s[p[i]].x - s[p[ans]].x) + abs(s[p[i]].y - s[p[ans]].y);
}
add(pp, s[p[i]].x + s[p[i]].y, i);
}
}
void change(int n) {
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < n; j++) {
if (i == 2 || i == 4)
swap(s[p[j]].x, s[p[j]].y);
else if (i == 3)
s[p[j]].x = -1 * s[p[j]].x;
}
mandis(n);
}
}
int main() {
int h, w;
h = read(), w = read();
int id = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
s[i * w + j].x = read();
s[i * w + j].pos = id++;
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
s[i * w + j].y = read();
}
}
int cnt;
for (int i = 0; i < h; i++) {
cnt = 0;
for (int j = 0; j < w; j++)
p[cnt++] = i * w + j;
change(cnt);
}
for (int j = 0; j < w; j++) {
cnt = 0;
for (int i = 0; i < h; i++) {
p[cnt++] = i * w + j;
}
change(cnt);
}
for (int i = 1; i <= h * w; i++)
fa[i] = i;
sort(edg, edg + tot, cmp2);
ll ans = 0;
for (int i = 0; i < tot; i++) {
if (findd(edg[i].u) != findd(edg[i].v)) {
Union(edg[i].u, edg[i].v);
ans += edg[i].val;
}
}
printf("%lld\n", ans);
return 0;
}