【poj2728】Desert King(最优比率生成树)

本文介绍了一种基于二分搜索和Prim最小生成树算法的最优比率生成树算法,该算法用于解决给定村庄间的水管建设问题,目标是最小化建设成本与总长度的比例。

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.

After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.

His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can’t share a lifter. Channels can intersect safely and no three villages are on the same line.

As King David’s prime scientist and programmer, you are asked to find out the best solution to build the channels.

Input

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1.000

题目大意:给出n个村庄的坐标和高度,给这n个村庄修n-1根水管,连接起n个村庄,两个村庄之间修水管的花费是高度差,距离是欧几里得距离,要求修的水管的花费和/距离和最小。
题解:求最优比率生成树,即当做01分数规划来想。最常规的做法是二分,二分比值,当花费/距离有贡献时选这个边,否则不选。也可以用迭代法,速度会更快。
最小生成树要用prim做,因为边有N²条。

代码如下:(迭代法)

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define ll long long
#define inf 1e15
#define N 1005
#define eps 1e-6 
using namespace std;
int n;
double Edge[N][N],lowcost[N];
int nearvex[N];
struct Point
{
    int x,y,z;
}p[N];
double cal(int a,int b)
{
    return sqrt(1.0*(p[a].x-p[b].x)*(p[a].x-p[b].x)+1.0*(p[a].y-p[b].y)*(p[a].y-p[b].y));
}
double prim(int src,double l)
{
    double cost=0,len=0;
    for(int i=1;i<=n;i++)
    {
        nearvex[i]=src;
        lowcost[i]=abs(p[src].z-p[i].z)-Edge[src][i]*l;
    }
    nearvex[src]=-1;
    for(int i=1;i<n;i++)
    {
        double mi=inf;
        int v=-1;
        for(int j=1;j<=n;j++)
        if(~nearvex[j] && lowcost[j]<mi)
        {
            v=j;
            mi=lowcost[j];
        }
        if(~v)
        {
            cost+=abs(p[nearvex[v]].z-p[v].z);
            len+=Edge[nearvex[v]][v];
            nearvex[v]=-1;
            for(int j=1;j<=n;j++)
            {
                double tmp=abs(p[v].z-p[j].z)-Edge[v][j]*l;
                if(~nearvex[j] && tmp<lowcost[j])
                {
                    lowcost[j]=tmp;
                    nearvex[j]=v;
                }
            }
        }
    }
    return cost/len;
}
int main()
{
    while(~scanf("%d",&n) && n)
    {
        for(int i=1;i<=n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) Edge[i][j]=cal(i,j);
        double a=0,b;
        while(1)
        {
            b=prim(1,a);
            if(fabs(a-b)<eps) break;
            a=b;
        }
        printf("%.3f\n",b);
    }
    return 0;
}
跟网型逆变器小干扰稳定性分析与控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模与分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计与参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环与内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析与控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估与改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值