Roads and Libraries(最小生成树)

来源:互联网 发布:python导入自定义模块 编辑:程序博客网 时间:2024/05/17 21:14

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 .

  3. 题目大意: 一个国家的路和图书馆都被损坏了, 脑残国王想让每个城市都能有或者通过游历别的国家到达图书馆, 给你几个点和几条被损坏的路和修一个图书馆和修一条路的花费,让你判断最小花费。
    这道题卡了我至少一个点!我的大体思路就是一次类似最小生成树操作, 得出最小生成树的边数(包括各个连通分量的)和联通分量数, 然后就有两种方案:要么全建图书馆, 要么在一个连通分量里建一个图书馆并把其余路给修通。 一直感觉思路没问题! 赛后试了各种数据, 终于发现了个BUG! Vector数组没清零!多么痛的领悟! 另外这个还有个坑点就是最后结果得用longlong, 不然会爆。 并查集貌似也能作。

    #include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <cstring>#include <queue>#define N 0x3f3f3f3f#define M 100100using namespace std;long long int n;int cr, cl;int m;vector <int> Map[M];int vis[M];//标记i是否访问int prim(int s);//prim算法找最小生成树int judge(int s);//判断是否还有未访问的点, 否则返回未访问点void judge2(int u, int v);//主要是判断Map里有没有重边int main(){    int q;    scanf("%d", &q);    while(q--)    {        memset(vis, 0, sizeof(vis));        scanf("%lld%d%d%d", &n, &m, &cl, &cr);        int u, v;        for(int i = 0; i < m; i++)        {            scanf("%d%d", &u, &v);            judge2(u, v);            judge2(v, u);        }        long long int e_cnt = prim(1);//敲黑板! 要用长整型存最小生成树边数, 不然最后一步计算会爆!        long long int v_cnt = 1;//同上, 存连通分量数        int x = 1;//记录遍历点        while(x)        {            x = judge(x);//从x点开始判断是否还有未访问的点, 有的话就连通分量数加一,e_cnt加上从这点开是最小生成树边数            if(!x)            {                break;            }            else            {                e_cnt += prim(x);                v_cnt ++;            }        }        long long int cost1 = e_cnt * cr + v_cnt * cl;//会爆int的一步。。。。        long long int cost2 = n * cl;//同上        printf("%lld\n", min(cost1, cost2));        for(int i = 0; i <= n; i++)        {            Map[i].clear();//vector 清零!多么痛的领悟!        }    }    return 0;}int prim(int s)//算是prim算法一个小小的变化吧{    queue <int> q;    int cnt = 0;    q.push(s);    vis[s] = 1;    while(!q.empty())    {        int t = q.front();        q.pop();        int S = Map[t].size();        for(int i = 0; i < S; i++)        {            int tt = Map[t][i];            if(!vis[tt])            {                vis[tt] = 1;                q.push(tt);                cnt ++;            }        }    }    return cnt;}int judge(int s){    for(int i = s; i <= n; i++)    {        if(!vis[i])            return i;    }    return 0;}void judge2(int u, int v){    int S = Map[u].size();    for(int i = 0; i < S; i++)    {        if(Map[u][i] == v)            return;    }    Map[u].push_back(v);}

原创粉丝点击