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. PL/SQL登录Oracle18数据库:ORA-12154:TNS无法解析指定的连接标识符

    PL/SQL登录Oracle18数据库出现ORA-12154:TNS无法解析指定的连接标识符解决以下问题:首先更改Oracle客户端的tnsnames.ora,我的路径是:D:\OracleSQL\n ...

  2. SAP WM TO Print Control设置里,Movement Type 的优先级更高

    SAP WM TO Print Control设置里,Movement Type 的优先级更高 存储类型的配置: 从storage type GRM 搬到任何地方,都不需要打印TO单. 移动类型的配置 ...

  3. CF1336C Kaavi and Magic Spell

    CF1336C Kaavi and Magic Spell 区间dp 题意 给一个长度为 \(n\) 的字符串 \(S\) 和一个长度为 \(m\) 的字符串\(T\) ,\(1\le m\le n\ ...

  4. python(递归函数)

    1.描述: 递归函数:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 2.递归函数特性: 必须有一个明确的结束条件: 每次进入更深一层递归时,问题规模相比上次递归 ...

  5. JavaWeb开发规范

    以下的建议将帮助你更有效地使用本文所描述的 Java 编程标准: ******************************************************* 当你写代码时就应该遵守 ...

  6. kafka-eagle监控kafka

    最近想做一个kafka监控,本来准备用zabbix来监控的,需要重复造轮子,本来准备用kafka-Manager的,在GitHub上无意发现了kafka-eagle,看了官方介绍准备试一下..... ...

  7. F - Qualification Rounds CodeForces - 868C 二进制

    F - Qualification Rounds CodeForces - 868C 这个题目不会,上网查了一下,发现一个结论就是如果是可以的,那么两个肯定可以满足. 然后就用二进制来压一下这个状态就 ...

  8. D - 小Z的加油店 线段树+差分+GCD

    D - 小Z的加油店 HYSBZ - 5028   这个题目是一个线段树+差分+GCD 推荐一个差分的博客:https://www.cnblogs.com/cjoierljl/p/8728110.ht ...

  9. python学习之组成字符串的两种方式

    第一种就是加法,比如 a ='张三' b = '李四' 那么print c =a+b 例如之前提到的 或者数值转换成字符串的 num  = 100 str(num) 其他参照表格中的转换即可 2.组成 ...

  10. 王颖奇 201771010129《面向对象程序设计(java)》第七周学习总结

    实验七 继承附加实验 实验时间 2018-10-11 1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: A.仅对本类可见-private B.对所有类可见-public C.对本包和 ...