并查集( Roads and Libraries locked)
来源:互联网 发布:淘宝的游戏藏宝湾 编辑:程序博客网 时间:2024/06/06 01:59
The Ruler of HackerLand believes that every citizen of the country should have access to a library. Unfortunately, HackerLand was hit by a tornado that destroyed all of its libraries and obstructed its roads! As you are the greatest programmer of HackerLand, the ruler wants your help to repair the roads and build some new libraries efficiently.
HackerLand has cities numbered from to . The cities are connected by bidirectional roads. A citizen has access to a library if:
- Their city contains a library.
- They can travel by road from their city to a city containing a library.
The following figure is a sample map of HackerLand where the dotted lines denote obstructed roads:
The cost of repairing any road is dollars, and the cost to build a library in any city is dollars.
You are given queries, where each query consists of a map of HackerLand and value of and .
For each query, find the minimum cost of making libraries accessible to all the citizens and print it on a new line.
Input Format
The first line contains a single integer, , denoting the number of queries. The subsequent lines describe each query in the following format:
- The first line contains four space-separated integers describing the respective values of(the number of cities), (the number of roads), (the cost to build a library), and (the cost to repair a road).
- Each line of the subsequent lines contains two space-separated integers, and , describing a bidirectional road connecting cities and .
Constraints
- Each road connects two distinct cities.
Output Format
For each query, print an integer denoting the minimum cost of making libraries accessible to all the citizens on a new line.
Sample Input
23 3 2 11 23 12 36 6 2 51 33 42 41 22 35 6
Sample Output
412
Explanation
We perform the following queries:
HackerLand contains cities connected by bidirectional roads. The price of building a library is and the price for repairing a road is .
The cheapest way to make libraries accessible to all is to:
- Build a library in city at a cost of .
- Repair the road between cities and at a cost of .
- Repair the road between cities and at a cost of .
This gives us a total cost of . Note that we don't need to repair the road between cities and because we repaired the roads connecting them to city !
In this scenario it's optimal to build a library in each city because the cost of building a library () is less than the cost of repairing a road ().
There are cities, so the total cost is .
就是并查集的应用,当见图书馆的钱小于修路的钱时,直接输出城镇数乘以图书馆数。否则, 就利用并查集将城镇分类,每一个集合有一个图书馆,其他的均修路。
#include <bits/stdc++.h>#define N 100005using namespace std;int pre[N];int find(int t) //查找根节点{ if(t == pre[t]) return t; //返回根节点 return pre[t] = find(pre[t]); //压缩路径}int main(){ int q, n, m; long long cl, cr, x, y, a, b; cin >> q; while(q--) { cin >> n >> m >> cr >> cl; if(cr <= cl) { printf("%lld\n", cr * n); for(int i = 0; i < m; i++) cin >> x >> y; } else { long long ans = 0; for(int i = 0; i <= n; i++) pre[i] = i; for(int i = 0; i < m; i++) { cin >> x >> y; a = find(x); b = find(y); //寻找根节点 if(a != b) //两者不连通将其联通 { pre[a] = b; ans += cl; } } for(int i = 1; i <= n; i++) if(pre[i] == i) ans += cr; printf("%lld\n", ans); } } return 0;}
- 并查集( Roads and Libraries locked)
- hackerrank Roads and Libraries(DFS/并查集)
- Locked Boxes (并查集)
- 1102 Constructing Roads(并查集)
- Roads and Libraries(最小生成树)
- Roads and Libraries
- Roads and Libraries
- Roads and Libraries HackerRank
- Jungle Roads(最小生成树+并查集)
- poj-2421-Constructing Roads(应用并查集)
- hdu 1301 Jungle Roads (并查集+快排)
- hdu 1102 Constructing Roads(并查集)
- HDU 1102 Constructing Roads (裸的并查集)
- zoj 1046(Jungle Roads)Kruskal 并查集实现
- Codeforces 362D Fools and Foolproof Roads【并查集+优先队列+思维】
- 1406 Jungle Roads(kruskal, 并查集)
- 【POJ】1251 - Jungle Roads(克鲁斯塔尔,并查集)(坑题)
- CF-25D - Roads not only in Berland(并查集或者搜索)
- 数据库概述
- mybatis实用注解
- vim 常用技巧
- Vuforia的学习(七)如何让游戏内摄像机跟着手机陀螺仪旋转
- 操作系统原理课后习题8.8
- 并查集( Roads and Libraries locked)
- JSP页面的组成
- 不要去眼红别人的高工资,重要的是把技术练到家,既然选择了前端,就要坚持在这条路上走下去
- 使用GhostDoc为.Net项目自动生成帮助文档
- Spring带给我们的好处
- Hyperopt 安装
- 题目三 蛇形填数(二)
- HTML5中代码调试,快捷键F10和F11区别
- jquery如何让页面的滚动条返回到顶部