small tree

Problem Description
There is a rooted tree which has N nodes numbered form 0 to N-1.Root is labeled 0.Each edge connects two nodes with a weight. Your job is to find S, a set of nodes {s1, s2…. sm} (0<=m<N), satisfying that:
a) Root is not in S, which means,0<si<N,1<=i<=m;
b) There is only one common ancestor between si and sj, which means, they have no common ancestor except root.
c) There are two associate set W, {w1,w2….wm}, and D, {d1,d2….dm},wi is the sum of weights of the path from root to si, di is the edge numbers of the path from root to si. The average outcome of S = ∑wi / ∑di (1<=i<=m) is maximal.
Input
There is a number T in the first line which is the number of test cases.
Each case begins with a integer n (2<=n<=1000), the number of nodes of the tree
Next n-1 lines each contains three integers i, j, k, indicating there is a directed edge from i to j with weight k.
Output
Output a floating point number for each case, which is the maximum average weight of S. Exact to 0.01.
Sample Input
3
1
2
0 1 2
3
0 1 1
0 2 2
Sample Output
0.00
2.00

2.00

#include<stdio.h>
#include<string.h>
#define maxn 1010
int map[maxn][maxn];
int n;
double len;
void dfs(int nd,double edge,double weight)
{
	if(len<weight/edge)
				len=weight/edge;
	int i;
	for(i=1;i<n;i++)
	{
		if(map[nd][i])
		{
			dfs(i,edge+1.0,weight+map[nd][i]*1.0);
		}
	}
}
int main()
{
	//freopen("b.txt","r",stdin);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(map,0,sizeof(map));
		scanf("%d",&n);
		int i,a,b,c;
		for(i=1;i<n;i++)
		{
			scanf("%d %d %d",&a,&b,&c);
			map[a][b]=c;
		}
		len=0.0;
		dfs(0,0.0,0.0);
		printf("%.2lf\n",len);
	}
	return 0;
}



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值