Roads and Libraries HackerRank

来源:互联网 发布:apache tomcat 7.0 7.0 编辑:程序博客网 时间:2024/06/07 07:50

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:

image

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:

  1. HackerLand contains cities connected by bidirectional roads. The price of building a library is and the price for repairing a road is .
    image

    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 !

  2. 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 (). image

    There are cities, so the total cost is .


这个题的大概意思是,让各个城市的人都能到图书馆,有两种方案,该城市有图书馆或者与该城市联通的城市有图书馆,已知图书馆的造价和修路的价格给出m条路,没联通城市默认修图书馆,求最少要用多少钱

路的条数加上图书馆的个数>=城市个数所以当图书馆造价比修路所需钱少的时候直接在每个城市修一个图书馆是最佳方案。


#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;long long int s[999999];long long f(long long x)//查找根节点{    if(x == s[x])        return x;    else    {        s[x] = f(s[x]);        return s[x];    }}int main(){    long long int q, i, n, m, l, r, cl, cr, sum, mon, x, y;    cin>>q;    while(q--)    {        sum = 0;        mon = 0;        cin>>n>>m>>cl>>cr;        for(i = 1;i <= n;i++)            s[i] = i;        for(i = 0;i < m;i++)        {            cin>>l>>r;            x = f(l);            y = f(r);            if(x != y)                s[y] = s[x];        }        if(cl <= cr)//修图书馆的价格比修路价格低的时候直接修图书馆。            mon = cl * n;        else        {            for(i = 1;i <= n;i++)//标记与下表相同的城市个数等于,连通图的个数            {                if(s[i] == i)                    sum++;            }            mon = cr * (n - sum) + sum * cl;        }        cout<<mon<<endl;    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 跳高比赛最终成绩相等怎么办 热车1200怠速降不下来怎么办 大腿根骨髓水肿越来越疼怎么办 倒库方向打晚了怎么办 签吻芳颜祛斑液脸脱皮怎么办 3d右边工具栏消失了怎么办 3d菜单栏消失了怎么办 觉得自己性无能不敢谈对象怎么办 护士面试时被问到病人坠床怎么办 三次元仪器坏了怎么办 运动同手同脚怎么办 狗狗突然害怕不敢走路怎么办 一岁半宝宝因为害怕不敢走路怎么办 猫把背拱起来怎么办 穿猫跟鞋走路不稳怎么办 狗狗后腿内八字怎么办 快走后小腿粗了怎么办? 猫的嘴巴烂了怎么办 苹果8丢了已关机怎么办 肚子吃多了难受怎么办 喝水喝的肚子胀怎么办 肚子吃撑了难受怎么办 肚子撑得想吐怎么办 吃饭吃的太饱怎么办 吃饭吃的太撑怎么办 跑步迈不开步子怎么办 踏步走步子反了怎么办 微信不支持计步怎么办 肝癌二次介入后头晕心慌怎么办 跑步跑得胃疼怎么办 如果世界上的猪都死了怎么办 我和我老婆吵架怎么办 我和我老婆吵架了怎么办 智障人士父母死后怎么办 依赖性太强的人怎么办 高考只有一门写准考证号怎么办 ps做动画不流畅怎么办 ff15引擎剑卖了怎么办 fgo从者满了怎么办 游泳池的水喝了怎么办 月球没有水怎么办秒懂