POJ 3925 Minimal Ratio Tree 最小生成树

5个免费股票数据API实测对比:从AkShare到BaoStock,哪个最适合你的AI量化项目? 本文深度评测了AkShare、BaoStock、yfinance、Alpha Vantage和EOD Historical Data这五个免费股票数据API,从数据质量、覆盖范围、更新频率、稳定性及易用性进行对比。针对AI量化项目,文章提供了详细的选型策略,帮助开发者根据项目阶段与需求,精准选择最适合的数据源,并强调了多源验证与数据清洗的重要性。 阅读详情

Description

For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation.

Ratio=\frac{\sum{edgeweight}}{\sum{nodeweight}}


Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a tree, which is a sub-graph of the original graph, with m nodes and whose ratio is the smallest among all the trees of m nodes in the graph.

Input

Input contains multiple test cases. The first line of each test case contains two integers n (2<=n<=15) and m (2<=m<=n), which stands for the number of nodes in the graph and the number of nodes in the minimal ratio tree. Two zeros end the input. The next line contains n numbers which stand for the weight of each node. The following n lines contain a diagonally symmetrical n×n connectivity matrix with each element shows the weight of the edge connecting one node with another. Of course, the diagonal will be all 0, since there is no edge connecting a node with itself.

All the weights of both nodes and edges (except for the ones on the diagonal of the matrix) are integers and in the range of [1, 100].

The figure below illustrates the first test case in sample input. Node 1 and Node 3 form the minimal ratio tree.

Output

For each test case output one line contains a sequence of the m nodes which constructs the minimal ratio tree. Nodes should be arranged in ascending order. If there are several such sequences, pick the one which has the smallest node number; if there's a tie, look at the second smallest node number, etc. Please note that the nodes are numbered from 1.

Sample Input

3 2 30 20 10 0 6 2 6 0 3 2 3 0 2 2 1 1 0 2 2 0 0 0

Sample Output

1 3 1 2

#include<cstdio>
#include<cmath>
using namespace std;
#define M 30
#define MAX 900000000
int w[M],g[M][M];
int n,m;
int ans[M];
int node[M];
double minans;
void prim()
{
    bool used[M];
    int dis[M];
    int i,j,nd=1;
    double totale,totaln;
    totale=totaln=0;
    for(i=0;i<m;i++)
    {
        used[i]=false;
        dis[i]=MAX;
    }
    used[0]=true;
    for(i=0;i<m;i++)
    {
        dis[i]=g[ node[0] ][ node[i] ];
    }
    while(nd<m)
    {
        int min=MAX;
        int mini=0;
        for(i=0;i<m;i++)
        {
            if(!used[i]&&dis[i]<min)
            {
                min=dis[i];
                mini=i;
            }
        }

        used[mini]=true;
        totale+=min;
        nd++;

        for(i=0;i<m;i++)
        {
            if(!used[i]&&g[ node[mini] ][ node[i] ] < dis[i])
            {
                dis[i] = g[ node[mini] ][ node[i] ];
            }
        }
    }
    for(i=0;i<m;i++)
    {
        totaln+=w[ node[i] ];
    }
    double now=totale/totaln;
    if(abs(now-minans)>0&&now<minans)
    {
        minans=now;
        for(i=0;i<m;i++)
        {
            ans[i]=node[i];
        }
    }
}
void fun(int ns,int num)
{
    if(ns==m)
    {
        prim();
        return ;
    }
    for(int i=num;i<n;i++)
    {
        node[ns]=i;
        fun(ns+1,i+1);
    }
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
        {
            break;
        }
        for(i=0;i<n;i++)
        {
            scanf("%d",w+i);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&g[i][j]);
            }
        }
        minans=MAX;
        fun(0,0);

        printf("%d",ans[0]+1);
        for(i=1;i<m;i++)
        {
            printf(" %d",ans[i]+1);
        }
        putchar(10);
    }
    return 0;
}

信贷审批中的那些坑:从零开始搭建风控策略体系的5个关键步骤 本文深入探讨了信贷审批中的风控策略体系搭建,从数据陷阱识别、规则冲突诊断到反欺诈攻防,提供了5个关键步骤的实战指南。重点解析了如何通过特征工程、决策树优化和模型可解释性技术,构建动态平衡的风控体系,有效降低逾期风险并提升审批效率。 阅读详情

相关推荐

abaqus运行报错 WindowsError:[Error 2]

abaqus提交job总是提示“WindowsError [Error 2”,报这个错误,可能是由于安装其他软件改变了abaqus软件的路径导致,总结了看到的教程,按照下面的方法可以解决这个问题。 步骤一:找到你的abaqus安装路径(我是按照一个安装视频安装的,安装路径为SIMULI1),然后找到SIMULIA文件夹,点击进入后找到commands文件夹,将这个路径复制下来。 步骤二:我的电脑右键属性,在左侧中找到高级系统设置并...

az101za的博客 1万+

HDU-2489-Minimal Ratio Tree(DFS+最小生成树 Prim)

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all...

qq_43461168的博客 219

osgEarth shapefile矢量生成三维模型案例 2.boston.earth

osgEarth shapefile矢量生成三维模型案例 2.boston.earth 波士顿城市建筑、路灯、公园、道路shp矢量生成三维模型案例。

qq_24934183的博客 3546

Minimal Ratio Tree

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n...

EyeT Hexleo 269

python 携程登陆_Python 学习练习之用户登录接口

目的:用python实现简单用户注册,登陆,加黑功能,放个流程图吧。以下内容仅为练习 执行流程:[root@zabbix login]# python user_login.pyusername:tompassword:123456Are you want to register ? [y/n]yRegister successful,Please relogin ![root@zabbix lo...

weixin_28679635的博客 519

使用Python下载ECMWF的ERA-5数据

一、注册账号 [官网]https://www.ecmwf.int/ 二、登录后获取下载key https://cds.climate.copernicus.eu/api-how-to 如图右侧红色框中的内容复制保存为TXT文件,并重命名为“.cdsapirc”,注意前面有点,保存在 C:\Users\username\.cdsapirc,(WINDOWS系统),username指当前电脑登录账户。 三、安装cdsapi库 以pycharm为例: 在pycharm中依次打开:File>setting

wise_sun的博客 7709

POJ3925 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1071   Accepted: 389 Description For a tree, which nodes and edges are all weighted, the r

mowayao的专栏 896

数字摄影与摄像中的图像信号处理器(ISP):流程与经典算法详解

图像信号处理器(Image Signal Processor, ISP)是现代数字成像系统(如智能手机、数码相机、网络摄像头等)中的核心组件,扮演着将图像传感器(如CMOS或CCD)捕获的原始光电信号转换为高质量、可供观看或进一步处理的数字图像的关键角色。在数字摄影和摄像的整个链路中,光线首先通过镜头(Lens)聚焦到图像传感器上,传感器将光信号转换为模拟电信号,随后经过模数转换(A/D转换)变为原始的数字图像信号。

qq_44682523的博客 3731

HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2382    Accepted Submission(s): 709 Problem Description For a tr

长风的专栏 1173

FPGA远程升级避坑指南:用AXI Quad SPI IP读写N25Q128 Flash的完整流程

本文详细介绍了FPGA远程升级过程中使用AXI Quad SPI IP读写N25Q128 Flash的完整流程,包括IP核配置、Flash操作指令集、双镜像备份设计及生产级远程升级协议。通过最佳实践和避坑策略,帮助开发者实现可靠的镜像更新方案,提升系统可靠性和可维护性。

weixin_30542079的博客 676

Linkage Mapper 工具参数详解——Building Network and Map Linkages

此文档用于解析使用LinkageMapper各输入输出参数详情以及可能的影响,并介绍了如何解释模型输出结果和输出参数,适合刚入手的人。篇幅很长很啰嗦,是因为每个参数都解释的万分细致。

sumuses的博客 6122

HDU Minimal Ratio Tree

Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes

jjs98 717

(9-2)离线强化学习:离线强化学习的主流方法

本文介绍了三种离线强化学习的主流约束策略优化方法:BCQ、CQL和Fisher-BRC,并通过CartPole环境进行性能比较。BCQ利用生成模型约束动作选择并引入扰动网络;CQL通过正则化Q值抑制对未见状态-动作对的高估;Fisher-BRC使用Fisher散度正则化策略分布。实验结果表明,三种方法各有优势,其中CQL表现最为稳定。文章还指出,为获得更好的性能,需要更大的数据集和更长的训练轮次。这些方法通过限制策略优化范围,有效提高了离线强化学习的稳定性和泛化能力。

码农三叔 1545

POJ 3925 Minimal Ratio Tree(枚举+最小生成树)

POJ 3925 Minimal Ratio Tree 题目链接 题意:给定一些点权和一个边权矩阵,求一个最小的比例的树 思路:先枚举用哪些点,然后求最小生成树即可 代码: #include #include #include using namespace std; const int N = 20; int n, m, val[N], edge[N][N

Remilia's 1486

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ 3925 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1303   Accepted: 479 Description For a tree, which nodes and edges are all weighted, the

Nightonke's Blog 622

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

poj_3925 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 836 Accepted: 304 题目链接:http://poj.org/problem?id=3925 Description For a tree, w

优快云515的专栏 804

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

链接: HDU : http://acm.hdu.edu.cn/showproblem.php?pid=2489 POJ  : http://poj.org/problem?id=3925 题目: Problem Description For a tree, which nodes and edges are all weighted, the ratio of

D_Double's Journey 3050

HDU2489 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2570 Accepted Submission(s): 760 Problem Description For a tree, whic

eeeaaaaa的专栏 490

HDU 2489 Minimal Ratio Tree (最小比例生成树)

题目链接 For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation.    Given a complete graph of n nodes with all nodes and edges weighted,...

不二君 374

hdu Minimal Ratio Tree(最小生成树---prim)

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1285    Accepted Submission(s): 383 Problem Description For a tr

勿忘初衷 2161

HDU 2489

题目: Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1140    Accepted Submission(s): 348 Problem Description For a

拂晓 949

HDU - 2489 + 最小生成树+暴力组合选数

For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is...

Become hunger become strong 483

HDU 2489 Minimal Ratio Tree(最小生成树)

该题就是最小生成树算法的变形,由于这个比值没有什么规律,不可能一下子算出最小情况,我们可以很容易发现,结点数非常少,所以我们可以枚举出m个结点的所有组合,这样,结点权值只和就确定了,为了使得比值最小,那么就要使得边权值之和最小,也就是最小生成树模板了。 枚举n个数中的m个可以有两种方法: dfs和二进制枚举子集。 该题我用的二进制,感觉比较方便。 细节参见代码: #include #inc

AC_Arthur的专栏 685
上一篇: POJ 3835 Columbus's bargain 最短路
下一篇: 最长回文子串
MakingMaker
博客等级 码龄15年 6粉丝 108原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值