首页 > ACM题库 > HDU-杭电 > hdu 2489 Minimal Ratio Tree-状态压缩-[解题报告]C++
2014
01-26

hdu 2489 Minimal Ratio Tree-状态压缩-[解题报告]C++

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, 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 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.

输出:

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.

样例输入:

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

样例输出:

1 3
1 2

思路:

直接状态压缩暴力枚举

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define Maxn 30
#define inf 10000000
using namespace std;
int ans,n,m;
int c,d;
int map[Maxn][Maxn];
int lis[Maxn],node[Maxn];
int val[1<<17],sum[1<<17];
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m),n||m){
        memset(val,48,sizeof(val));
        memset(sum,0,sizeof(sum));
        c=1000000,d=1;
        for(i=1;i<=n;i++)
            scanf("%d",&node[i]);
        memset(map,0,sizeof(map));
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                scanf("%d",&map[i][j]);
            }
        }
        int N=1<<n;
        N-=1;
        int cnt=0;
        val[0]=0;
        for(i=0;i<=n;i++)
            val[1<<i]=0;
        for(i=1;i<=N;i++){
            cnt=0;
            for(j=0;j<n;j++){
                if(i&(1<<j)) lis[++cnt]=j+1,sum[i]+=node[j+1];
            }
            if(cnt>m) continue;
            if(cnt==m)
            {
                int a=val[i],b=sum[i];
                if(a*d<c*b){
                    ans=i;
                    c=a;
                    d=b;
                }
            }
            int temp;
            for(j=0;j<n;j++){
                if(((1<<j)&i)==0){
                    temp=inf;
                    for(int k=1;k<=cnt;k++){
                        temp=min(temp,map[j+1][lis[k]]);
                    }
                    val[i|(1<<j)]=min(val[i]+temp,val[i|(1<<j)]);
                }
            }
        }
        int num=1;
        for(j=0;j<n;j++)
            if((1<<j)&ans){
                printf("%d",j+1);
                if(num!=m)
                    printf(" ");
                num++;
            }
        printf("\n");
    }
    return 0;
}

 

解题转自:http://www.cnblogs.com/wangfang20/archive/2013/08/25/3280920.html


  1. 人肉结果:深圳侮辱无辜女子的警察姓名:陈军,籍贯,湖北武汉人,1968年生,1987年入伍,原在深圳武警7支队服役,从排长一路升上到支队长,后转业到宝安公安分局工作,在武警服役期间就是一贯的喜欢调戏妇女,并在外有多名情人。………………知情战友宣

  2. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.