CodeForces - 618D Hamiltonian Spanning Tree(思维)
2018-03-30 14:31
726 查看
Hamiltonian Spanning TreeA 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.InputThe 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.OutputPrint a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.ExamplesinputCopy
.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条边边权为x且构成一棵树,其余边权为y,问每个点走一次遍历所有点最小花费.
思路:当y<= x,有一种情况是必须走一条x边,就是某一点与其他所有点的连线边权均为x,否则都走y即可,很好理解.
当x< y,由于每个点只能走一次,也就是入一次,出一次,共两条边,其他边都走不了,所以我们从任意点开始深搜一遍,
A->B这条边能不能走要看B有没有把两个度花完,所以我们可以写成一个回溯,如果从B回溯回来的值是0说明这条边不能走,否则可以走.在每个dfs函数里要记录有没有把两个度花完.(真**的脑洞)
代码:#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;
int n,x,y;
vector<int> mp[maxn];
int cnt;
int dfs(int x,int f)
{
int k = mp[x].size();
int d = 2;
for(int i = 0;i< k;i++)
{
if(mp[x][i] == f)
continue;
int tmp = dfs(mp[x][i],x);
if(tmp&&d> 0)
{
d--;
cnt++;
}
}
return d> 0?1:0;
}
int main()
{
cin>>n>>x>>y;
for(int i = 1;i< n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
if(x< y)
{
dfs(1,-1);
printf("%lld\n",(ll)x*cnt+(ll)y*(n-1-cnt));
}
else
{
int flag = 0;
for(int i = 1;i<= n;i++)
{
if(mp[i].size() == n-1)
flag = 1;
}
if(flag)
printf("%lld\n",(ll)x+(ll)y*(n-2));
else
printf("%lld\n",(ll)y*(n-1));
}
return 0;
}
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.InputThe 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.OutputPrint a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.ExamplesinputCopy
5 2 3 1 2 1 3 3 4 5 3output
9inputCopy
5 3 2 1 2 1 3 3 4 5 3output
8NoteIn 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条边边权为x且构成一棵树,其余边权为y,问每个点走一次遍历所有点最小花费.
思路:当y<= x,有一种情况是必须走一条x边,就是某一点与其他所有点的连线边权均为x,否则都走y即可,很好理解.
当x< y,由于每个点只能走一次,也就是入一次,出一次,共两条边,其他边都走不了,所以我们从任意点开始深搜一遍,
A->B这条边能不能走要看B有没有把两个度花完,所以我们可以写成一个回溯,如果从B回溯回来的值是0说明这条边不能走,否则可以走.在每个dfs函数里要记录有没有把两个度花完.(真**的脑洞)
代码:#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;
int n,x,y;
vector<int> mp[maxn];
int cnt;
int dfs(int x,int f)
{
int k = mp[x].size();
int d = 2;
for(int i = 0;i< k;i++)
{
if(mp[x][i] == f)
continue;
int tmp = dfs(mp[x][i],x);
if(tmp&&d> 0)
{
d--;
cnt++;
}
}
return d> 0?1:0;
}
int main()
{
cin>>n>>x>>y;
for(int i = 1;i< n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
if(x< y)
{
dfs(1,-1);
printf("%lld\n",(ll)x*cnt+(ll)y*(n-1-cnt));
}
else
{
int flag = 0;
for(int i = 1;i<= n;i++)
{
if(mp[i].size() == n-1)
flag = 1;
}
if(flag)
printf("%lld\n",(ll)x+(ll)y*(n-2));
else
printf("%lld\n",(ll)y*(n-1));
}
return 0;
}
相关文章推荐
- Codeforces 550C - Divisibility by Eight(思维)
- codeforces 777C Alyona and Spreadsheet(预处理+思维)
- codeforces 699B One Bomb (思维)
- CodeForces 732D Exams (思维+set乱搞)
- Codeforces 466D Increase Sequence【思维】好题~
- CodeForces - 814B(思维)
- Codeforces-194C-Cutting Figure (思维+bfs)
- Codeforces 463D Gargari and Permutations【思维建图+Floyd跑最长路】不愧是CF的评测姬
- Codeforces 518C Anya and Smartphone【思维+模拟】
- Codeforces 552D Vanya and Triangles【暴力枚举+思维】
- CodeForces - 758C Unfair Poll (模拟+暴力+思维)
- Codeforces 501C:Misha and Forest(好题+思维+异或的运算规律+树)
- CodeForces 714C Sonya and Queries(字典树/思维)
- codeforces 500c New Year Book Reading 【思维】
- CodeForces - 729D Sea Battle(思维题)
- CodeForces - 818D Multicolored Cars(思维)
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 574D Bear and Blocks【思维】
- Codeforces 387D George and Interesting Graph【思维+二分匹配】
- Codeforces 763B Timofey and rectangles(四色定理)(思维)