Roads and Libraries HackerRank - torque-and-development

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

2
3 3 2 1
1 2
3 1
2 3
6 6 2 5
1 3
3 4
2 4
1 2
2 3
5 6

Sample Output

4
12

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值