2014
01-26

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;
}

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

2. [email protected]