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. 【手把手教你】win10 虚拟机 VMware Workstation Pro 15下安装Ubuntu 19.04

    虚拟机 VMware Workstation Pro 15.5.0 及永久激活密钥 https://www.cnblogs.com/zero-vic/p/11584437.html Ubuntu19. ...

  2. 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  3. iOS开发之结合asp.net webservice实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...

  4. 图论--二分图最佳完美匹配(KM模板)

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; cons ...

  5. MySQL 索引、视图

    1.索引 什么是索引 一个索引是存储在表中的数据结构,索引在表的列名上创建.索引中包含了一个列的值,这些值保存在一个数据结构中 索引优缺点 索引大大提高了查询速度 会降低更新表的速度,如对表进行INS ...

  6. linux上github的简单使用

    Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.目前,包括Rubinius ...

  7. Dreamoon Likes Coloring(模拟+构造)

    \(这题刚好撞到我的思路了,但是因为模拟......我看了几十遍测试数据....\) $首先当\sum_^m$小于n时一定无解 大于呢?那我们就要浪费一些区间(覆盖一些点,也就是多出来的点) 但是又不 ...

  8. Spring官网阅读(十二)ApplicationContext详解(中)

    文章目录 1.Spring的资源(Resource) 接口简介 UML类图 抽象基类AbstractResource FileSystemResource AbstractFileResolvingR ...

  9. SpringCloudGateWay学习 之 从函数式编程到lambda

    文章目录 前言: 函数式编程: 什么是函数式编程: 函数式编程的特点 lambda表达式: 核心: 函数接口: 方法引用: 类型推断: 变量引用: 级联表达式跟柯里化: 前言: 这一系列的文章主要是为 ...

  10. 测试开发专题:spring-boot统一异常捕获

    java异常介绍 异常时相对于return的一种退出机制,可以由系统触发,也可由程序通过throw语句触发,异常可以通过try/catch语句进行捕获并处理,如果没有捕获,则会导致程序退出并输出异常栈 ...