题目链接
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
PS:题意很简单,就是在n个节点中选取m个点,然后找出权值之和和边权之和最小的m个点。因为n最大只有15,所以可以直接暴力跑出答案,细节看代码。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e2+10;
const int mod=1e9+7;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
int m,n,a[maxn],maps[maxn][maxn];
int chose[maxn],ans[maxn];
double mi;
double prime(int x)//求m个点边权之和
{
int d[maxn],sum=0;fill(d,d+maxn,inf);
d[x]=0;
bool vis[maxn];me(vis,0);
for(int i=1;i<=m;i++)
{
int mi=inf,u=-1;
for(int j=1;j<=n;j++)
if(chose[j]&&!vis[j]&&d[j]<mi)
mi=d[j],u=j;
vis[u]=1,sum+=mi;
for(int j=1;j<=n;j++)
if(chose[j]&&!vis[j]&&maps[u][j]<d[j])
d[j]=maps[u][j];
}
return sum*1.0;
}
void dfs(int x,int s)
{
if(s==m)//刚好选了m个点
{
int flog=0,l=1;
double sum1,sum2=0;
for(int i=1;i<=n;i++)
if(chose[i])
{
if(!flog)
flog=1,sum1=prime(i);//以第一个点为起点,查询该m个点的边权之和
sum2+=a[i]*1.0;
}
double sum=sum1/sum2;
if(sum<mi)//去比例最小的m个点
{
mi=sum;
for(int i=1;i<=n;i++)
if(chose[i])
ans[l++]=i;
}
return ;
}
chose[x]=1;
dfs(x+1,s+1);
chose[x]=0;
if(n-x+s>=m)//在剩下的节点中仍可以选出m个点,可跳过当前点,选剩下的点
dfs(x+1,s);
}
int main()
{
while(scanf("%d%d",&n,&m)&&m+n)
{
me(chose,0),mi=inf;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&maps[i][j]);
dfs(1,0);
for(int i=1;i<m;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[m]);
}
return 0;
}