並查集( Roads and Libraries locked)

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 .

就是並查集的應用,當見圖書館的錢小於修路的錢時,直接輸出城鎮數乘以圖書館數。否則, 就利用並查集將城鎮分類,每一個集合有一個圖書館,其他的均修路。

#include <bits/stdc++.h>
#define N 100005

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章