九度 题目1144:Freckles

本文详细解析了一个使用克鲁斯卡尔算法解决最短连通路径问题的实例。通过输入一系列坐标点,算法计算出连接所有点的最短路径总长度,展示了并查集和排序在最小生成树构建中的应用。
题目描写叙述:

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 
    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle. 

输入:

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

例子输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
例子输出:
3.41

题目简单翻译一下就是:输入n为节点数;然后以下n行是每一个节点的坐标。然后输出是这n个节点的连通最短路径;

AC的代码例如以下:

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int fin[100];
struct node{//节点类。用来存储节点信息
    double x;
    double y;
}nod[100];

struct edge{//边的类,设置起点编号总是小于终点编号,避免了反复计算边
    int start;
    int end;
    double side;
}edg[5000];

double fun(node a, node b){//求边的距离长度
    double num = (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
    return sqrt(num);
}

bool cmp(edge e1, edge e2){//比較函数。用来将边的长度依照从小到大排序
    return e1.side < e2.side;
}

int find(int a){//并查集的运用
    if(fin[a] == a){
        return a;
    }
    return find(fin[a]);
}

void Merge(int a,int b){//并查集的运用
    a = find(a);
    b = find(b);
    fin[a] = fin[b];
}

int main(){
    int n;
    double a,b;
    while(scanf("%d",&n) != EOF){
        //初始化
        for(int i = 0; i < n; i++){
            scanf("%lf%lf",&a,&b);
            nod[i].x = a;
            nod[i].y = b;
            fin[i] = i;
        }
        int index = 0;
        for(int i = 0; i < n; i++){
            for(int j = i + 1; j < n; j++){
                edg[index].start = i;
                edg[index].end = j;
                edg[index].side = fun(nod[i],nod[j]);
                index++;
            }
        }
        sort(edg, edg + index, cmp);//对边进行排序
        double sum = 0;//用来计算最短连通路径长度
        //開始选边,从小到大開始选,运用的是克鲁斯卡尔算法
        for(int i = 0; i < index; i++){
            int a = edg[i].start;
            int b = edg[i].end;
            if(find(a) != find(b)){
                Merge(a,b);
                sum = sum + edg[i].side;
            }
        }
        printf("%.2lf\n",sum);
    }
}

转载于:https://www.cnblogs.com/ljbguanli/p/7008098.html

内容概要:本文围绕“计及V2G主动支撑的光伏-储能-电动汽车输配协同日前优化调度”展开研究,提出了一种综合考虑光伏发电、储能系统与电动汽车(EV)在V2G(Vehicle-to-Grid)模式下协同参与电网调度的优化模型。通过Matlab代码实现,构建了日前优化调度框架,充分挖掘电动汽车作为移动储能单元的潜力,利用其双向充放电能力为主动配电网提供调峰、填谷和备用等主动支撑服务。研究综合考虑了可再生能源出力不确定性、负荷需求波动以及电动汽车出行行为特征,建立了多主体、多目标的协同优化机制,旨在降低系统运行成本、提高新能源消纳水平,并增强电网运行的稳定性与可靠性。该资源属于电力系统与综合能源系统领域的高水平科研复现资料,具备较强的理论深度与工程应用价值; 适合人群:具备电力系统分析、优化建模基础及Matlab编程能力的研究生、科研人员,以及从事智能电网、能源互联网、综合能源系统等相关领域技术研发的专业技术人员; 使用场景及目标:①用于学习和复现源-网-荷-储协同优化调度的核心建模方法与求解流程;②掌握V2G技术在电网调频调峰中的数学建模方法及其在优化调度中的集成应用;③支撑光伏、储能与电动汽车耦合系统的低碳经济调度、鲁棒优化或分布鲁棒优化等前沿课题的研究与仿真验证; 阅读建议:建议结合文中提供的Matlab代码与主流优化工具箱(如YALMIP、CPLEX、Gurobi等)进行实践操作,重点理解目标函数设计、约束条件构建及多变量耦合关系的处理策略,同时可进一步拓展至日内滚动优化、实时调度或多时间尺度协调优化方向开展深入研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值