Minimal Ratio Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2180 Accepted Submission(s): 630
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
Source
这道题是2008年北京现场比赛的一道题,题意大致意思是给定n个节点的一个图,要你从中选出这小边的权值和除以节点权值和的最小的一个树
于是很好理解的为最小生成树,采用普利姆最小生成树....注意精度的问题,这里我wa了n次
哎,喵了个咪
代码:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define max 0x3f3f3f3f
#define maxn 17
int node_weight[maxn];
int edge_weight[maxn][maxn];
int depath[maxn]; //以这些点形成一颗最小生成树
int m , n ;
double res;
int stu[maxn];
int sub_map[maxn][maxn];
void Prime()
{
int vis[maxn]={};
int lowc[maxn];
int i,j,k,minc;
double ans=;
for(i=;i<=m;i++) //从n中挑出m个点形成一个子图
{
for(j=;j<=m;j++)
{
if(edge_weight[depath[i]][depath[j]]==)
sub_map[i][j]=max;
else
sub_map[i][j]=edge_weight[depath[i]][depath[j]];
}
}
vis[]=;
for(i=;i<=m;i++)
{
lowc[i]=sub_map[][i];
}
for(i=;i<=m;i++)
{
minc=max;
k=;
for(j=;j<=m;j++)
{
if(vis[j]==&&minc>lowc[j])
{
minc=lowc[j];
k=j;
}
}
if(minc==max) return ; //表示没有联通
ans+=minc;
vis[k]=;
for(j= ; j<=m;j++)
{
if(vis[j]==&&lowc[j]>sub_map[k][j])
lowc[j]=sub_map[k][j];
}
}
int sum=;
for(i=;i<=m;i++) //统计点权值的和
sum+=node_weight[depath[i]];
ans/=sum;
if(res+0.00000001>=ans)
{
if((res>=ans&&res<=ans+0.000001)||(res<=ans&&res+0.000001>=ans+0.000001))
{
for(i=;i<=m;i++)
{
if(stu[i]<depath[i]) return;
}
}
res=ans;
memcpy(stu,depath,sizeof(depath));
}
}
void C_n_m(int st ,int count)
{
if(count==m)
{
Prime();
return ;
}
for(int i=st ;i<=n;i++ )
{
depath[count+]=i;
C_n_m(i+,count+);
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),m+n)
{
for(i=;i<=n;i++)
scanf("%d",node_weight+i); //记录节点权值
for(i=;i<=n;i++) //记录边权值
for(j=;j<=n;j++)
scanf("%d",&edge_weight[i][j]);
// C(n,m)
res=max;
C_n_m(,);
for(i=;i<=m;i++)
{
printf("%d",stu[i]);
if(i!=m)printf(" ");
}
putchar();
}
return ;
}