Hamiltonian Spanning Tree CodeForces - 618D (思维)

本文探讨了一个由n个城市组成的网络,每两个城市间都有一条无向道路相连的问题。介绍了如何选择一个包含n-1条边的生成树,并将这些边的通过时间从y秒调整为x秒的方法。目标是最小化遍历所有城市的总时间,给出了解决方案并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example
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 .

代码

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>

using namespace std;
int n,x,y;
int num=0;
vector<int>graph[200005];
bool dfs(int root,int fa)
{
    int d=2;
    for(int i=0;i<graph[root].size();i++)
    {
        int v=graph[root][i];
        if(v==fa)
            continue;
        if(dfs(v,root)&&d)
          num++,d--;
    }
    return d>0;
}
int main()
{
    cin>>n>>x>>y;
    int sign=false;
    int p1,p2;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&p1,&p2);
        graph[p2].push_back(p1);
        graph[p1].push_back(p2);
        if(graph[p1].size()==n-1)
            sign=true;
        if(graph[p2].size()==n-1)
            sign=true;
    }
    if(x>y)
    {
        if(sign)
            num++;
    }
    else
        dfs(1,-1);
    cout<<(long long)num*x+(long long)(n-1-num)*y<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值