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
解题报告
这道题的题意就是在n个点中找m个点使得这m个点构成的生成树的边权与点权之比最小。
由于其数据范围相当小,为了枚举m个点的所有集合,我们直接写一个dfs来枚举。并且把找到的m个点存储到一个数组里头(也可以用vector)。然后找到了这m个点之后,直接跑一遍Kruskal找出最小生成树的边权和(因为这m个点已经确定了)。不断更新答案找到最小的那个就行了。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=200;
const double INF=10005.0;
int num,fig;
int n,m;
int nw,ew;
int node[N+5];
int map[N+5][N+5],father[N+5];
int vis[N+5],out[N+5];
struct edge
{
int u,v,w;
}ed[2*N+5];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int getfather(int x)
{
return (father[x]==x)?x:(father[x]=getfather(father[x]));
}
inline int merge(int x,int y)
{
return father[getfather(x)]=getfather(y);
}
inline void kruskal()
{
int tot=0;
int nw1=0,ew1=0;
for(int i=1;i<=n;i++)father[i]=i;
for(int i=1;i<=n;i++)
if(vis[i])nw1+=node[i];
for(int i=1;i<=num;i++)
{
int u=ed[i].u,v=ed[i].v;
if(vis[u]&&vis[v]&&getfather(u)!=getfather(v))//除了不再一棵树里,还要满足在dfs找到的那个集合里
{
merge(ed[i].u,ed[i].v);
tot++;
ew1+=ed[i].w;
}
if(tot==m-1)break;
}
if(nw*ew1<ew*nw1)//更新答案
{
fig=0;
for(int i=1;i<=n;i++)
if(vis[i])out[++fig]=i;
nw=nw1;ew=ew1;
}
}
void dfs(int pos,int f)//找一找有m个点的集合
{
if(pos==m+1)
{
kruskal();
return ;
}
for(int i=f+1;i<=n;i++)
{
vis[i]=1;
dfs(pos+1,i);
vis[i]=0;
}
}
int main()
{
while(true)
{
scanf("%d%d",&n,&m);
if(n==0&&m==0)break;
num=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
scanf("%d",&node[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if(j>=i)continue;
ed[++num].u=i;
ed[num].v=j;
ed[num].w=map[i][j];
}
sort(ed+1,ed+1+num,cmp);
nw=1;ew=1500;
dfs(1,0);
for(int i=1;i<=fig;i++)
{
if(i==1)printf("%d",out[i]);
else printf(" %d",out[i]);
}
printf("\n");
}
return 0;
}