UVALive 3662 Another Minimum Spanning Tree

同poj3241

#include<cstdio>
#include<algorithm>
#define N 100010
#define INF 0x3f3f3f3f
#define LL long long

using namespace std;

struct point
{
    int x,y,id;
    bool operator<(const point &p) const
    { return x==p.x?y<p.y:x<p.x;}
}p[N];
struct edge
{
    int a,b,w;
    bool operator<(const edge &x) const
    { return w<x.w;}
}e[N<<2];
struct BIT
{
    int w,p;
}bit[N];

int n,m,tot;
int f[N],a[N],b[N];

int abs(int x)
{
    return x<0?-x:x;
}

int find(int x)
{
    return x==f[x]?x:f[x]=find(f[x]);
}

int query(int x)
{
    int r=INF,p=-1;
    while(x<=m)
    {
        if (bit[x].w<r) r=bit[x].w,p=bit[x].p;
        x+=x&-x;
    }
    return p;
}

void update(int x, int w, int p)
{
    while(x>0)
    {
        if (bit[x].w>w) bit[x].w=w,bit[x].p=p;
        x-=x&-x;
    }
}

void addedge(int a, int b, int w)
{
    ++tot;
    e[tot].a=a,e[tot].b=b,e[tot].w=w;
}

int dist(point a, point b)
{
    return abs(a.x-b.x)+abs(a.y-b.y);
}

int main()
{
    int Case=0;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        tot=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            p[i].id=i;
        }
        for (int dir=1;dir<=4;dir++)
        {
            if (dir==2||dir==4)
                for (int i=1;i<=n;i++) swap(p[i].x,p[i].y);
            else  if (dir==3)
                for (int i=1;i<=n;i++) p[i].x=-p[i].x;
            sort(p+1,p+n+1);

            for (int i=1;i<=n;i++) a[i]=b[i]=p[i].y-p[i].x;
            sort(b+1,b+1+n);
            m=unique(b+1,b+1+n)-b-1;
            for (int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+m+1,a[i])-b;

            for (int i=1;i<=m;i++) bit[i].w=INF,bit[i].p=-1;
            for (int i=n;i>=1;i--)
            {
                int pos=query(a[i]);
                if (pos!=-1) addedge(p[i].id,p[pos].id,dist(p[i],p[pos]));
                update(a[i],p[i].x+p[i].y,i);
            }
        }
        LL ans=0;
        sort(e+1,e+tot+1);
        for (int i=1;i<=n;i++) f[i]=i;
        for (int i=1;i<=tot;i++)
            if (find(e[i].a)!=find(e[i].b))
        {
            f[find(e[i].a)]=find(e[i].b);
            ans+=e[i].w;
        }
        printf("Case %d: Total Weight = %lld\n",++Case,ans);
    }
    return 0;
}

  

时间: 2024-11-09 01:48:54

UVALive 3662 Another Minimum Spanning Tree的相关文章

UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小生成树

题目链接:点击打开链接 题意: 给定二维平面的n个点坐标,问曼哈顿MST 的值. 模版题 #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <vector> #incl

LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)

题目大意: 曼哈顿最小距离生成树 算法讨论: 同上. 这回的模板真的准了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 const int N = 100000 + 5; 8 const int M = N * 8; 9 type

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树

寻找图中最小连通的路径,图如下: 算法步骤: 1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. 3. Repeat st

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX

CF609E. Minimum spanning tree for each edge

题解:随便构造一颗最小生成树 然后对于其他不在树上的边  考虑到 删除这条链上的最大值在把这条边加上去 能得到这条边所在的最小生成树 可以LCT维护 但是明显这个题是静态的树就没必要LCT 当然我觉得最优的是树剖以后ST nlogn的的复杂度 也可以树剖+线段树nlog^2的复杂度 #include <bits/stdc++.h> const int MAXN=2e5+10; #define ll long long using namespace std; ll read(){ ll x=0

【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情况下,我们把图看成是一种由"顶点"和"边"组成的抽象网络.在各个"顶点"间可以由"边"连接起来,使两个顶点间相互关联起来.图的结构可以描述多种复杂的数据对象,

[思维]Minimum Spanning Tree

题目描述 In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G. Precisely speaking, for an undire