比赛 HAOI2009 模拟试题1 评测结果 AWWWWWWWWW
题目名称 洞窟探索 最终得分 10
用户昵称 苏轼 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2011-03-25 21:17:14
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>

using namespace std;

FILE *fin = fopen("hole.in", "r"),
	 *fout = fopen("hole.out", "w");

struct TNODE {
	int end, len;
} map[1510][3];

int buf[1510][1010], child[1510][2];
int M, N, sum, ct = 1;

bitset<1510> boo = 1;

int *dp (const int wh)
{
	int *f = buf[ct++];

	boo[wh] = 1;
	int *g[2] = {buf[0], buf[0]}, len[2] = {0}, c = 0;
	for (int i=0; i<3; i++)
		if (!boo[map[wh][i].end])
		{
			len[c] = map[wh][i].len;
			child[wh][c] = child[wh][map[wh][i].end]+1;
			g[c++] = dp(map[wh][i].end);
			if (c == 2)
				break;
		}

	memset(f, 0x2F, (M+1)*sizeof(int));
	f[1] = 0;
	for (int a=1; a<=M; a++)
		for (int b=1; b<a && b<=child[wh][0]; b++)
		{
			int k = g[0][b] + g[1][a-b-1] + 
					b*(M-b)*len[0] + (a-b-1)*(M-(a-b-1))*len[1];
			if (k < f[a])
				f[a] = k;
		}

	return f;
}

int main ()
{
	fscanf(fin, "%d %d\n", &N, &M);
	sum = M*(M-1)/2;

	memset(buf[0], 0x3F, (M+1)*sizeof(int));
	buf[0][0] = 0;
	for (int i=1; i<N; i++)
	{
		int X;
		TNODE nn;
		fscanf(fin, "%d %d %d\n", &X, &nn.end, &nn.len);
		if (!map[X][0].len)
			map[X][0] = nn;
		else if (!map[X-1][1].len)
			map[X][1] = nn;
		else
			map[X][2] = nn;
	}

	int *ret = dp(1);
	fprintf(fout, "%.2f\n", ret[M]/((double)sum));

	return 0;
}