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

被折叠的 条评论
为什么被折叠?



