• 洛谷1268 树的重量


    原题链接

    (n = 2)时,显然答案为(M(1, 2))
    (n = 3)时,我们固定(M(1, 2)),考虑(3)的位置。

    如图,(3)的位置肯定是从((1,2))中分支出来的,所以答案为(M(1, 2) + dfrac{M(1, 3) + M(2, 3) - M(1, 2)}{2})
    于是我们可以拓展到(n)的情况,第(n)个点可以从((1, 2 o n - 1))中分支出来,然后我们可以贪心地选择,即这条分支的长为(min limits _{i = 2} ^ {n - 1} { dfrac{M(1, n) + M(i, n) - M(1, i)}{2} })
    所以最后的答案为$$M(1, 2) + sum limits _{i = 3} ^ {n} min limits _{j = 2} ^ {i - 1} { dfrac{M(1, i) + M(j, i) - M(1, j)}{2} }$$

    #include<cstdio>
    using namespace std;
    const int N = 31;
    int a[N][N];
    inline int re()
    {
    	int x = 0;
    	char c = getchar();
    	bool p = 0;
    	for (; c < '0' || c > '9'; c = getchar())
    		p |= c == '-';
    	for (; c >= '0' && c <= '9'; c = getchar())
    		x = x * 10 + c - '0';
    	return p ? -x : x;
    }
    inline int minn(int x, int y){ return x < y ? x : y; }
    int main()
    {
    	int i, j, n, s, mi;
    	while (1)
    	{
    		n = re();
    		if (!n)
    			return 0;
    		for (i = 1; i < n; i++)
    			for (j = i + 1; j <= n; j++)
    				a[i][j] = re();
    		s = a[1][2];
    		for (i = 3; i <= n; i++)
    		{
    			mi = 1e9;
    			for (j = 2; j < i; j++)
    				mi = minn(mi, a[1][i] + a[j][i] - a[1][j]);
    			s += mi >> 1;
    		}
    		printf("%d
    ", s);
    	}
    	return 0;
    }
    
  • 相关阅读:
    2021年下半年北京市中小学教师资格考试笔试报名公告
    高效演讲
    php的Allowed memory size of 134217728 bytes exhausted问题解决办法
    1111error
    http 500 错误
    xshell连接centons
    Vue 计算属性
    Vue 自定义指令
    Vue 事件绑定
    Vue v-cloak指令解决插值表达式“闪动”问题
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9899864.html
Copyright © 2020-2023  润新知