Roads and Libraries
来源:互联网 发布:一元秒杀包邮 淘宝 编辑:程序博客网 时间:2024/06/05 06:45
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 .
题意:给你n个城镇,m条路,然后是c1图书馆的价格,c2修路的价格,让你求如果这n个乡镇的人都想看书,那么最省钱的做法是什么
首先用并查集确定一下一个集合的人数,然后判断修路与修图书馆的价格。如果修路的价格比修图书馆还要贵,那么直接全部修图书馆。反之就修一个图书馆
其他的都修路。
坑点:输出结果用longlong;对于没有提到的节点,直接修图书馆
#include<bits/stdc++.h>using namespace std;int pre[100860];int sum[100860];int vis[100860];int findboss(int x){ if(x==pre[x]) return x; else { pre[x]=findboss(pre[x]); return pre[x]; }}int main(){ int t; scanf("%d",&t); while(t--) { int n,cbook,crode; long long m; scanf("%d%lld%d%d",&n,&m,&cbook,&crode); for(int i=1;i<=n;i++) pre[i]=i,sum[i]=1,vis[i]=0;; while(m--) { int u,v; scanf("%d%d",&u,&v); vis[u]=vis[v]=1; int aa=findboss(u); int bb=findboss(v); if(aa!=bb) { pre[aa]=bb; sum[bb]+=sum[aa]; sum[aa]=1; } } long long summ=0; for(int i=1;i<=n;i++) { if(sum[i]!=1) { // printf("%d\n",sum[i]); if(crode>cbook) summ+=sum[i]*cbook; else summ+=((sum[i]-1)*crode+cbook); } if(!vis[i]) { summ+=cbook; } } printf("%lld\n",summ); }}
- Roads and Libraries
- Roads and Libraries
- Roads and Libraries HackerRank
- Roads and Libraries(最小生成树)
- 并查集( Roads and Libraries locked)
- hackerrank Roads and Libraries(DFS/并查集)
- Static and dynamic libraries
- USACO Roads and Planes
- codeforces567E. President and Roads
- Generate Libraries and BSPs 成功
- Animation Scripts, Libraries and Plugins
- MinGW Static and Dynamic Libraries
- Linker and Libraries Guide(1)
- Fools and Foolproof Roads CodeForces
- Building and Installing ACE and Its Auxiliary Libraries and Services
- Linker and Libraries Guide Appendix D Linker and Libraries Updates and New Features
- Static, Shared Dynamic and Loadable Linux Libraries
- Asset Libraries and Blocks in iOS 4
- 华为P6安装Sygic
- 【51Nod 1789】跑的比谁都快
- 多线程中线程停止的问题
- Java中常用的设计模式
- USACO-Section1.5 SuperPrime Rib [质数]
- Roads and Libraries
- 西邮杯初赛
- PAT甲级 1006
- 分享马化腾在3Q大战后写给腾讯全体员工的一封信
- C++设计模式之观察者模式
- 简单使用makefile V1.0
- 数通平台软件中的概念:组件
- 你所不知道的腾讯和马化腾——一封腾讯内部的员工信
- 多线程应用场景