Underground Cables

Underground Cables

Description

A city wants to get rid of their unsightly power poles by moving their power cables underground. They have a list of points that all need to be connected, but they have some limitations. Their tunneling equipment can only move in straight lines between points. They only have room for one underground cable at any location except at the given points, so no two cables can cross. 

Given a list of points, what is the least amount of cable necessary to make sure that every pair of points is connected, either directly, or indirectly through other points? 

Input

There will be several test cases in the input. Each test case will begin with an integer N(2<=N<=1, 000), which is the number of points in the city. On each of the next N lines will be two integers, X and Y(- 1, 000<=X, Y<=1, 000), which are the (X, Y) locations of the N points. Within a test case, all points will be distinct. The input will end with a line with a single 0. 

Output

For each test case, output a single real number, representing the least amount of cable the city will need to connect all of its points. Print this number with exactly two decimal places, rounded. Print each number on its own line with no spaces. Do not print any blank lines between answers. 

Sample Input

4
0 0
0 10
10 0
10 10
2
0 0
10 10
0

Sample Output

30.00
14.14

题意:链接所有点的最小值
解法:最小生成树

#include <iostream>
using namespace std;
#include <math.h>
#include <stdio.h>

struct Node{
    double val;
    int x, y;
    void operator=(Node& other) {
        val = other.val;
        x = other.x;
        y = other.y;
    }
};

int n, m, f[2000], a[2000][3], tot;
Node heap[2000000];

int sqr(int x) {
    return x*x;
}

void up(int x) {
    while (x > 1 && heap[x].val < heap[x/2].val) {
        Node temp;
        temp = heap[x];
        heap[x] = heap[x/2];
        heap[x/2] = temp;
        x = x/2;
    }
}

void down(int x) {
    while (1) {
        if (x*2 > tot) break;
        int o = x*2;
        if (o+1 <= tot && heap[o+1].val < heap[o].val) o = o+1;
        if (heap[o].val < heap[x].val) {
            Node temp;
            temp = heap[x];
            heap[x] = heap[o];
            heap[o] = temp;
            x = o;
        } else break;
    }
}

int get(int x) {
    if (x == f[x]) return x;
    f[x] = get(f[x]);
    return f[x];
}

int main() {
    //freopen("h.in","r",stdin);

    cin >> n;
    while (n != 0) {
        for (int i = 1; i <= n; i++) {
            cin >> a[i][1] >> a[i][2];
            f[i] = i;
        }
        tot = 0;
        for (int i = 1; i <= n; i++)
            for (int j = i+1; j <= n; j++) {
                double dis = sqrt(sqr(a[i][1]-a[j][1])+sqr(a[i][2]-a[j][2]));
                tot++;
                heap[tot].val = dis;
                heap[tot].x = i;
                heap[tot].y = j;
                up(tot);
            }
        double ans = 0;
        for (int k = 1; k <= n-1; k++) {
            while (get(heap[1].x) == get(heap[1].y)) {
                heap[1] = heap[tot];
                tot--;
                down(1);
            }
            ans = ans+heap[1].val;
            int fx = get(heap[1].x), fy = get(heap[1].y);
            f[fx] = fy;
            heap[1] = heap[tot];
            tot--;
            down(1);
        }
        printf("%.2lf\n", ans);
        cin >> n;
    }

    //fclose(stdin);
}
### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向和速度。在数学上,光流场可以表示为像素位置和时间的一阶导数,即Ex、Ey(空间梯度)和Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函数负责计算光流场。该函数接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey`和`Et`分别代表沿x轴、y轴和时间轴的像素强度变化;`gray1`和`gray2`用于存储当前帧和前一帧的平均灰度值;`u`则表示计算出的光流矢量大小。 #### 图像处理流程 1. **初始化和预处理**:`memset`函数被用来清零`opticalflow`数组,它将保存计算出的光流数据。同时,`output`数组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性和效率。 3. **光流向量计算**:通过比较当前帧和前一帧的灰度值,计算出每个像素点的Ex、Ey和Et值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算出的光流值被存储在`opticalflow`数组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`数组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函数将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函数中,通过调用`cvCalcOpticalFlowLK`函数实现了这一算法,该函数接受前一帧和当前帧的灰度图,以及窗口大小等参数,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解和优化光流算法,可以进一步提升视频分析的准确性和实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉和理解动态场景中的运动信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值