G
N
I
D
A
O
L

天梯赛-交通运输


天梯赛-交通运输

#include 
#include 
#include 
using namespace std;
typedef long long LL;
const int N = 200005;
const int INF = 0x3f3f3f3f;
int n, cnt[N], pre[N];
LL res, sum[N];
struct Road
{
    int x, y;
    int w;
    bool operator<(const Road &a)
    {
        return w > a.w;
    }
} road[N];
void init(int n)
{
    for (int i = 1; i <= n; i++)
        pre[i] = i, cnt[i] = 1;
}
int find(int key)
{
    return pre[key] == key ? key : pre[key] = find(pre[key]);
}
int main()
{
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 1; i < n; i++)
            cin >> road[i].x >> road[i].y >> road[i].w;
        sort(road + 1, road + n);
        init(n);
        fill(sum, sum + N, res = 0);
        for (int i = 1; i < n; i++)
        {
            int rootx = find(road[i].x), rooty = find(road[i].y);
            LL getx = cnt[rooty] * road[i].w + sum[rootx];
            LL gety = cnt[rootx] * road[i].w + sum[rooty];
            if (getx > gety)
            {
                pre[rooty] = rootx;
                cnt[rootx] += cnt[rooty];
                sum[rootx] = getx;
            }
            else
            {
                pre[rootx] = rooty;
                cnt[rooty] += cnt[rootx];
                sum[rooty] = gety;
            }
            res = max(res, max(getx, gety));
        }
        cout << res << endl;
    }
    return 0;
}

文章作者: AnglesD
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 AnglesD !
评论
 上一篇
天梯赛-消消乐(简单) 天梯赛-消消乐(简单)
消消乐游戏开始有n个方块排成一行,每个方块有一个分数(分数有可能为负数)。游戏规则是可以选择若干对相邻的方块,让他们同时消除。每消除一对方块,要将游戏的分数加上这两个方块分数相乘的积,并且两个方块消除后还会占位,每个方块只能消除一次。
2021-07-10
下一篇 
天梯赛-摘棉花 天梯赛-摘棉花
新疆棉花到了采摘季节,小龙带着他的棉花收割机准备投入采摘工作,他计划要投入N个小时进行棉花采摘工作。请计算小龙在老郑的时间表规定的时间段内工作N小时可以采摘的棉花最大重量。
2021-07-10