Codeforces 618 D 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.

A 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 n, x, y 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 n, x 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
5 2 3
1 2
1 3
3 4
5 3
Output
9
Input
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 .

【题意】有n*(n-1)/2条边的完全图,边权为y,现在给了一颗最小生成树,树上的边的权值为x。现在要从某个点出发访问所有的顶点,问路径上经过的最小权值之和是多少?

【解题方法】思维&&脑洞。题解参考:http://blog.csdn.net/yp_2013/article/details/50629164

           首先经过每个点恰好一次,那么走的总边数是一定的,即n-1,那么既然边的权值只有两种,那么肯定是算出一种,然后用减法求得另外一种喽!
分为两种情况,如果最小生成树上的边小的话,那么肯定就是要贪心地在树上选最小的边!
一个点可以有入度和出度,那么每个节点就是最多连两条边,所以就在最小生成树上构造这样一条路径就ok了!
另一种情况,如果最小生成树是星形的话,(也就是某个节点的度数为n-1),那么无疑肯定要选择一条最小生成树上的边,其余的话都绝对可以用非树上的边去遍历!

【AC 代码】

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int>G[maxn];
bool used[maxn];
int degree[maxn];
void dfs(int u,int f,int &num)
{
    int cnt=0;
    for(auto v : G[u]){
        if(v==f) continue;
        dfs(v,u,num);
        cnt += used[v];
    }
    used[u] = cnt<2?1:0;
    num+=min(cnt,2);
}
int main()
{
    int n,x,y,u,v;
    cin>>n>>x>>y;
    for(int i=1; i<=n; i++) G[i].clear();
    memset(degree,0,sizeof(degree));
    for(int i=1; i<=n-1; i++){
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    if(x<y){
        memset(used,0,sizeof(used));
        int num=0;
        dfs(1,-1,num);
        printf("%lld\n",1LL*((1LL)*num*x+1LL*(n-1-num)*y));
    }
    else{
        bool ok=0;
        for(int i=1; i<=n; i++){
            if(degree[i]==n-1){
                ok=1;
                break;
            }
        }
        if(ok==1){
            printf("%lld\n",(1LL)*x+(1LL)*(n-2)*y);
        }
        else{
            printf("%lld\n",(1LL)*(n-1)*y);
        }
    }
    return 0;
}

【解题方法2 贪心】

            贪心的解法,c表示以这个点为根的下面的有效边为多少,然后dfs表示的是这个点能贡献出来的有限边是多少以及答案可以贡献出多少!

【AC 代码 】

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int>G[maxn];
int degree[maxn];
int n,x,y,num;
int dfs(int u,int v)
{
    int c=0;
    for(auto it : G[u]){
        if(it==v) continue;
        c += dfs(it,u);
    }
    if(c==0) return 1;
    if(c==1) {num++; return 1;}
    else{
        num+=2;
        return 0;
    }
}
int main()
{
    cin>>n>>x>>y;
    memset(degree,0,sizeof(degree));
    for(int i=1; i<=n; i++) G[i].clear();
    for(int i=1; i<=n-1; i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        degree[u]++,degree[v]++;
    }
    if(x>=y)
    {
        bool ok=0;
        for(int i=1; i<=n; i++){
            if(degree[i]==n-1){
                ok=1;
                break;
            }
        }
        if(ok) printf("%lld\n",(1LL)*x+(1LL)*y*(n-2));
        else printf("%lld\n",(1LL)*y*(n-1));
    }else{
        num=0;
        dfs(1,-1);
        printf("%lld\n",(1LL)*num*x+(1LL)*(n-1-num)*y);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值