2017ccpc全国邀请赛(湖南湘潭) H. Highway

本文介绍了一种解决特定图论问题的算法,即如何在给定的树形结构中找到最昂贵的方式连接所有节点。通过计算树的直径和从直径两端点出发的最远距离,可以构建出满足条件的树。文章提供了详细的算法步骤和C++实现代码。

H. Highway
In ICPCCamp there were n towns conveniently numbered with 1,2,...,n connected with (n−1) roads. The
i-th road connecting towns a i and b i has length c i . It is guaranteed that any two cities reach each other
using only roads.
Bobo would like to build (n − 1) highways so that any two towns reach each using only highways. Building
a highway between towns x and y costs him δ(x,y) cents, where δ(x,y) is the length of the shortest path
between towns x and y using roads.
As Bobo is rich, he would like to find the most expensive way to build the (n − 1) highways.
Input
The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains an integer n. The i-th of the following (n − 1) lines contains three integers a i , b i and
c i .
• 1 ≤ n ≤ 10 5
• 1 ≤ a i ,b i ≤ n
• 1 ≤ c i ≤ 10 8
• The number of test cases does not exceed 10.
Output
For each test case, output an integer which denotes the result.
Sample Input
5
1 2 2
1 3 1
2 4 2
3 5 1
5
1 2 2
1 4 1
3 4 1
4 5 2
Sample Output
19
15

官方题解:

先求出树的最远点对(树的直径)d1,d2,再求出以直径的两个端点为起点的dist[i](起点到i的距离),首先将直径(d1,d2的距离)加入集合,对于其他点i,加入max(d1到i的距离,d2到i的距离)到集合,集合所构成的树就是题目的答案

至于树的最远点对怎么求就懒得说了

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define max(x,y) (x)>(y)?(x):(y)
#define min(x,y) (x)>(y)?(y):(x)
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1<<28;
const int maxn=100010;
const int maxm=110;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n;
struct node
{
    int next,to,cost;
}edge[maxn*2];
int head[maxn],cnt;
void add(int st,int ed,int cost)
{
    edge[cnt].to=ed;
    edge[cnt].cost=cost;
    edge[cnt].next=head[st];
    head[st]=cnt++;
}
ll distd1[maxn],distd2[maxn];
int vis[maxn];
void dfs(int x,ll d,ll dist[])
{
    vis[x]=1;
    dist[x]=d;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(vis[edge[i].to]==0)
            dfs(edge[i].to,d+edge[i].cost,dist);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        cls(head,-1);
        cnt=0;
        for(int i=0;i<n-1;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        cls(vis,0);
        dfs(1,0,distd1);
        int d1,d2;
        ll maxdist=0;
        for(int i=1;i<=n;i++)
            if(distd1[i]>maxdist)
                maxdist=distd1[i],d1=i;

        cls(vis,0);
        dfs(d1,0,distd1);
        maxdist=0;
        for(int i=1;i<=n;i++)
            if(distd1[i]>maxdist)
                maxdist=distd1[i],d2=i;
        cls(vis,0);
        dfs(d2,0,distd2);
        ll ans=distd1[d2];
        for(int i=1;i<=n;i++)
        {
            if(i!=d1&&i!=d2)
                ans+=max(distd1[i],distd2[i]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/mgz-/p/6857985.html

内容概要:本文档详细介绍了基于直驱永磁同步发电机(PMSG)的1.5MW风力发电系统在Simulink环境下的建模与仿真全过程,涵盖了风力机空气动力学模型、PMSG电磁特性建模、不可控整流与逆变电路、直流环节、空间矢量脉宽调制(SVPWM)技术以及核心控制策略的设计。重点实现了最大功率点跟踪(MPPT)控制以提升风能捕获效率,并构建了电压外环与电流内环协同工作的双闭环控制系统,通过仿真验证了系统在不同风速条件下稳定运行的能力及动态响应性能。; 适合人群:适用于具备电力系统、电机控制理论基础及Simulink仿真操作经验的研究生、科研人员和从事新能源发电系统开发的工程技术人员;特别适合正在进行风电系统建模、控制算法研究或完成相关毕业设计的专业人士。; 使用场景及目标:①深入理解直驱式PMSG风力发电系统的整体架构与工作机理;②掌握从物理部件建模到控制策略实现的完整Simulink仿真流程;③学习并复现MPPT控制、双闭环控制等关键技术方案;④为后续开展低电压穿越、并网稳定性分析、故障诊断等高级课题提供可靠的仿真平台支撑。; 阅读建议:建议结合Matlab/Simulink软件动手实践,逐模块搭建模型,重点关注各控制环节的参数设计与调试方法,同时可参照文中提供的其他风电相关资源进行拓展学习与对比分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值