CF618D:Hamiltonian Spanning Tree(贪心 & 树形dp & 最小路径覆盖)

D. Hamiltonian Spanning Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are  roads in total. It takes exactly y seconds to traverse any single road.

spanning tree is a set of roads containing exactly n - 1 roads such that it's possible to travel between any two cities using only these roads.

Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from y to x seconds. Note that it's not guaranteed that x is smaller than y.

You would like to travel through all the cities using the shortest path possible. Given nxy and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once.

Input

The first line of the input contains three integers nx and y (2 ≤ n ≤ 200 000, 1 ≤ x, y ≤ 109).

Each of the next n - 1 lines contains a description of a road in the spanning tree. The i-th of these lines contains two integers ui and vi(1 ≤ ui, vi ≤ n) — indices of the cities connected by the i-th road. It is guaranteed that these roads form a spanning tree.

Output

Print a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.

Examples
input
Copy
5 2 3
1 2
1 3
3 4
5 3
output
9
input
Copy
5 3 2
1 2
1 3
3 4
5 3
output
8
Note

In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is .

In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is .


题意:一个完全图,给定一棵生成树,树的边权为x,不在树上的边权为y,找出一条路经过所有点一次且总权值最小。

思路:当x>=y时,显然可以全部走y边,围着树的重心跑就行,但是星形图除外,最后要跑一次x边,特判以上就行。对于x<y,既然总的边数是n-1,那么肯定尽量多跑树上的边了,也就是树的最小路径覆盖问题,因为已经特判了星形图,所以不会存在“孤立根节点”的情况。因此可以通过

①树形dp,dp[i]表示以i为根的答案,那么i要么是非转折点,要么是转折点,分别用0和1表示,xjb转移就行。

②贪心,比较经典的东西了。

# include <bits/stdc++.h>
# define pb push_back
using namespace std;
typedef long long LL;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+30;
LL dp[maxn][2];
vector<int>g[maxn];
int n, x, y, u, v;
void dfs(int cur, int pre)
{
    LL sum = 0, son=0, cnt1=0, cnt2=0, imin1=inf, imin2=inf, flag=0;
    for(int to:g[cur])
    {
        if(to == pre) continue;
        dfs(to, cur);
        if(g[to].size()>1) flag=1;
        if(dp[to][0] <= dp[to][1])
        {
            sum += dp[to][0];
            ++cnt1;
        }
        else if(dp[to][0] >= dp[to][1])
        {
            sum += dp[to][1];
            ++cnt2;
            if(dp[to][0]-dp[to][1] < imin1) imin2=imin1,imin1=dp[to][0]-dp[to][1];
            else if(dp[to][0]-dp[to][1]<imin2) imin2=dp[to][0]-dp[to][1];
        }
        ++son;
    }
    dp[cur][0] = dp[cur][1] = sum+son*y;
    if(cnt1) dp[cur][0] = min(dp[cur][0], sum+x+(son-1)*y);
    if(cnt2) dp[cur][0] = min(dp[cur][0], sum+imin1+x+(son-1)*y);
    if(cnt1>1) dp[cur][1] = min(dp[cur][1], sum+2*x+(son-2)*y);
    if(cnt2>1) dp[cur][1] = min(dp[cur][1], sum+imin1+imin2+2*x+(son-2)*y);
    if(cnt1 && cnt2) dp[cur][1] = min(dp[cur][1], sum+imin1+2*x+(son-2)*y);
}
int main()
{
    memset(dp, 0x3f, sizeof(dp));
    LL ans = inf;
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1; i<n; ++i)
    {
        scanf("%d%d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
    }
    if(x >= y)
    {
        for(int i=1; i<=n; ++i)
            if(g[i].size()==n-1)
                return 0*printf("%lld\n",1LL*(n-2)*y+x);
        return 0*printf("%lld\n",1LL*(n-1)*y);
    }
    dfs(1, 0);
    printf("%lld\n",min(dp[1][0],dp[1][1]));
    return 0;
}

# include <bits/stdc++.h>
# define pb push_back
using namespace std;
typedef long long LL;
const int maxn = 2e5+30;
vector<int>g[maxn];
bool mark[maxn];
int v[maxn];
void dfs(int cur,int pre)
{
	v[cur]=1;
	int tot=0;
	for(int to:g[cur])
	{
		if(to==pre)continue;
		dfs(to, cur);
		v[cur] += v[to];
		if(!mark[to])tot++;
	}
	if(tot>=2) v[cur]-=2, mark[cur]=1;
	else if(tot==1) --v[cur];
}
int main()
{
    int n, x, y;
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1, u, v; i<n; ++i)
    {
        scanf("%d%d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
    }
    if(x >= y)
    {
        for(int i=1; i<=n; ++i)
            if(g[i].size()==n-1)
                return 0*printf("%lld\n",1LL*(n-2)*y+x);
        return 0*printf("%lld\n",1LL*(n-1)*y);
    }
    dfs(1, 0);
    printf("%lld\n",1LL*(v[1]-1)*y+1LL*(n-v[1])*x);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值