比赛 20121107 评测结果
题目名称 小树 最终得分 42
用户昵称 Truth.Cirno 运行时间 0.195 s
代码语言 C++ 内存使用 11.92 MiB
提交时间 2012-11-07 11:47:35
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <memory.h>
using namespace std;

struct booltype
{
	bool boo[1010];
};

int n,sonnum[1010],son[1010][1010],soncost[1010][1010],deep[1010],dis[1010];
double maxnum;
booltype una,got,dad[1010],empty;

void fillit(int pos,int deepnow,int disnow,booltype dadnow)
{
	int i,to,cost;
	deep[pos]=deepnow;
	dis[pos]=disnow;
	dad[pos]=dadnow;
	for (i=0;i<sonnum[pos];i++)
	{
		to=son[pos][i];
		cost=soncost[pos][i];
		dadnow.boo[pos]=true;
		fillit(to,deepnow+1,disnow+cost,dadnow);
		dadnow.boo[pos]=false;
	}
}

void cal(void)
{
	int i;
	double ww=0,dd=0,ans;
	for (i=1;i<n;i++)
		if (got.boo[i])
		{
			ww+=dis[i];
			dd+=deep[i];
		}
	ans=ww/dd;
	maxnum=max(maxnum,ans);
}

void dfs(int pos,bool get)
{
	int i;
	booltype unat=una;
	got.boo[pos]=get;
	for (i=1;i<n;i++)
		if (una.boo[i]&&dad[pos].boo[i])
			return;
	//not return
	/**/
	if (pos==n-1)
	{
		cal();
		return;
	}
	/**/
	for (i=1;i<n;i++)
		una.boo[i]=una.boo[i]||dad[pos].boo[i];
	dfs(pos+1,0);
	dfs(pos+1,1);
	una=unat;
}

int main(void)
{
	freopen("treec.in","r",stdin);
	freopen("treec.out","w",stdout);
	int times,i,T,from,to,cost;
	cin>>T;
	for (times=1;times<=T;times++)
	{
		memset(sonnum,0,sizeof(sonnum));
		memset(son,0,sizeof(son));
		memset(soncost,0,sizeof(soncost));
		memset(deep,0,sizeof(deep));
		memset(dis,0,sizeof(dis));
		cin>>n;
		if (n<2)
		{
			cout<<"0.00"<<endl;
			continue;
		}
		for (i=1;i<n;i++)
		{
			cin>>from>>to>>cost;
			son[from][sonnum[from]]=to;
			soncost[from][sonnum[from]++]=cost;
		}
		fillit(0,0,0,empty);
		/*go*/
		maxnum=0;
		dfs(1,0);
		dfs(1,1);
		printf("%.2lf\n",maxnum);
	}
	return(0);
}