Minimal Ratio TreeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4462    Accepted Submission(s): 1411

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 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<vector>
#include<cstring>
#include<algorithm>
const int inf=1008611;
using namespace std;
int par[22],n,m,va[22],mp[22][22],num[22];
double rap;
bool Ju(int k)
{
    int tc=0;
    while(k)
    {
        if(k&1)
        {
            k|=1;
            ++tc;
        }
        k>>=1;
    }
    return tc==m;
}
void slove(int zt)
{
    double sv=0,se=0;
    vector<int>pt;
    int mark,low[22];
    bool vis[22];
    for(int i=0; i<n; ++i)
        if((1<<i)&zt)
        {
            vis[i]=0;
            low[i]=inf;
            sv+=va[i];
            pt.push_back(i);
        }
    mark=pt[0];
    vis[mark]=1;
    for(int i=1; i<m; ++i)
    {
        int u=mark,minn=inf;
        for(int j=0; j<m; ++j)
            if(low[pt[j]]>mp[u][pt[j]]) low[pt[j]]=mp[u][pt[j]];
        for(int j=0; j<m; ++j)
            if(minn>low[pt[j]]&&!vis[pt[j]])
            {
                minn=low[pt[j]];
                mark=pt[j];
            }
        se+=low[mark];
        vis[mark]=1;
    }
    double rs=se*1.0/sv;
    if(rs<rap)
    {
        for(int i=0; i<m; ++i)
            num[i]=pt[i];
            rap=rs;
    }
    else if(rs==rap)
    {
        bool rig=0;
        for(int i=0; i<m; ++i)
        {
            if(num[i]>pt[i])
            {
                rig=1;
                break;
            }
            else if(num[i]<pt[i]) break;
        }
        if(rig)
        {
            for(int i=0; i<m; ++i)
                num[i]=pt[i];
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d",&n,&m),n||m)
    {
        rap=inf;
        for(int i=0; i<n; ++i) scanf("%d",va+i);
        for(int i=0; i<n; ++i)
            for(int j=0; j<n; ++j)
            {
                scanf("%d",&mp[i][j]);
                if(i==j) mp[i][j]=inf;
            }
        for(int i=0; i<(1<<n); ++i)
            if(Ju(i)) slove(i);
        for(int i=0; i<m; ++i)
            if(i) printf(" %d",num[i]+1);
            else printf("%d",num[i]+1);
        puts("");
    }
}
 

最小生成树 状压+prim hdu2489的更多相关文章

  1. tyvj 2054 [Nescafé29]四叶草魔杖——最小生成树+状压dp

    题目:http://www.joyoi.cn/problem/tyvj-2054 枚举点集,如果其和为0,则作为一个独立的块求一下最小生成树.因为它可以不和别的块连边. 然后状压dp即可. 别忘了判断 ...

  2. [tyvj2054] 四叶草魔杖 (最小生成树 状压dp)

    传送门 Background 陶醉在彩虹光芒笼罩的美景之中,探险队员们不知不觉已经穿过了七色虹,到达了目的地,面前出现了一座城堡和小溪田园,城堡前的木牌上写着"Poetic Island&q ...

  3. HDU2489【状压枚举】

    题意: 给你n个点的图,然后让你在图里挑m个点,达到sumedge/sumnode最小 思路: 由于数据范围小,状压枚举符合m个点的状态,我是用vactor存了结点位置,也记录了结点的sum值,然后跑 ...

  4. 【10.26校内测试】【状压?DP】【最小生成树?搜索?】

    Solution 据说正解DP30行??? 然后写了100行的状压DP?? 疯狂特判,一算极限时间复杂度过不了aaa!! 然而还是过了....QAQ 所以我定的状态是待转移的位置的前三位,用6位二进制 ...

  5. 【Luogu】P4208最小生成树计数(状压乱搞)

    题目链接 最小生成树有两个性质,两个性质都知道的话这题就变成码农题了. 1.无论最小生成树长什么样,所有权值的边的数量是不变的.比如我有棵最小生成树有两条权值为2的边四条权值为1的边,那这个图的所有最 ...

  6. BZOJ 2595: [Wc2008]游览计划 [DP 状压 斯坦纳树 spfa]【学习笔记】

    传送门 题意:略 论文 <SPFA算法的优化及应用> http://www.cnblogs.com/lazycal/p/bzoj-2595.html 本题的核心就是求斯坦纳树: Stein ...

  7. [WC2008]游览计划(状压dp)

    题面太鬼畜不粘了. 题意就是给一张n*m的网格图,每个点有点权,有k个关键点,让你把这k个关键点连成一个联通快的最小代价. 题解 这题nmk都非常小,解法肯定是状压,比较一般的解法插头dp,但不太好写 ...

  8. BZOJ.3058.四叶草魔杖(Kruskal 状压DP)

    题目链接 \(2^{16}=65536\),可以想到状压DP.但是又有\(\sum A_i\neq 0\)的问题.. 但是\(2^n\)这么小,完全可以枚举所有子集找到\(\sum A_i=0\)的, ...

  9. bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp

    给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...

随机推荐

  1. zabbix自动监控钉钉报警

    钉钉报警 一:设置钉钉机器人  二:zabbix服务器server端配置 1.修改zabbix_server.conf文件 [root@server ~]# vim /usr/local/zabbix ...

  2. WLAN 无线网络 03 - RF 基础

    射频(Radio frequency),又称无线电频率.无线射频.高周波,常被用来当成无线电的同义词,为在3 kHz至300 GHz这个范围内的震荡频率,这个频率相当于无线电波的频率,以及携带着无线电 ...

  3. Spring绑定请求参数过程以及使用@InitBinder来注册自己的属性处理器

    在工作中,经常会出现前台的请求参数由于无法被正常转型,导致请求无法进到后台的问题. 比如,我有一个User.其性别的属性被定义成了枚举,如下: public enum Gender { MALE(&q ...

  4. jQuery学习(三)

    事件 on方法可以将一个事件绑定在jQuery对象上,当你的操作触发了这些事件时,便会调用你所绑定的函数. 例如,给某个超链接绑定点击事件. <head> <meta http-eq ...

  5. ffmpeg+SDL2实现的音频播放器V2.0(无杂音)

    1. 前言 目前为止,学习了并记录了ffmpeg+SDL2显示视频以及事件(event)的内容. 这篇中记录ffmpeg+SDL2播放音频,没加入事件处理. 接下来加入事件处理并继续学习音视频同步,再 ...

  6. webpack4.x下babel的安装、配置及使用

    前言 目前,ES6(ES2015)这样的语法已经得到很大规模的应用,它具有更加简洁.功能更加强大的特点,实际项目中很可能会使用采用了ES6语法的模块,但浏览器对于ES6语法的支持并不完善.为了实现兼容 ...

  7. 图论--LCA--在线RMQ ST

    板子测试POJ1330,一发入魂,作者是KuangBin神犇,感谢?‍ #include <cstdio> #include <cstring> #include <al ...

  8. AWVS 安全渗透扫描

    1.打开软件,点击 New Scan 2.在 website url 中输入被扫描的网址,点击 next 3.在 scanning profile 中选择测试的漏洞类型,默认选择 default(默认 ...

  9. UTC 时间转化为北京时间

    // UTC 时间转化为北京时间 function utc2beijing(utcTime) { var T_pos = utcTime.indexOf('T'); var Z_pos = utcTi ...

  10. C. Jury Marks 思维

    C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...