Nature Reserve(最小生成森林)

Nature Reserve

Kattis - naturereserve

In a Nature Reserve and Wildlife Park, there are NN environmental monitoring stations to monitor temperature, atmospheric pressure, humidity, fire, water quality, etc. Each station, labeled from 11 to NN, uses solar panels to supply energy for its operations. There is a communication network consisting of several two-way communication channels between pairs of stations. All stations are connected via this communication network.

To process data at each station, the Nature Reserve and Wildlife Park needs to install a Smart Data Analysis program (with the size of LL bytes) to all environmental monitoring stations. The program is initially installed directly to SS stations, then broadcast to and installed in all other stations via the communication network.

To save energy, all communication channels are initially in an idle state and need to be activated to send information. It takes Eij{E}_{ij} energy units to activate the communication channel between station ii and station jj. Once a channel is activated, it takes one energy unit to transmit one byte via this channel.

Your task is to determine the minimum energy units required to send the Smart Data Analysis program to all stations from the initial SS stations.

Input
The input consists of several datasets. The first line of the input contains the number of datasets, which is a positive number and is not greater than 2020. The following lines describe the datasets.

Each dataset is described by the following lines:

The first line contains four positive integers: the number of environmental monitoring stations NN, the number of two-way communication channels MM, the size of the program LL (in bytes), and the number of initial stations SS. The constraints are 1SN1041 \leq S \leq N \leq {10}^{4}, 1M1061 \leq M \leq {10}^{6}, MN(N1)/2M \leq N(N-1)/2, and 1L1061 \leq L \leq {10}^{6}.

The second line contains SS positive integer representing the initial SS stations.

Each of the following MM lines contains three positive integers i,ji, j and Eij{E}_{ij} to denote that there is a two-way communicataion channel between station ii and station jj, and it takes Eij{E}_{ij} energy units to activate this channel (Eij106)({E}_{ij} \leq {10}^{6}).

Output
For each data set, output the minimum energy units required to send the Smart Data Analysis program to all stations from the initial SS stations.

Sample Input 1
1
4 6 10 1
3
1 2 4
1 3 8
1 4 1
2 3 2
2 4 5
3 4 20

Sample Output 1
37

題意:n個點m條邊的無向圖,有s個初始點有文件,要把s個點的文件通過邊傳到其它所有點,每傳一個文件,耗費L,每條邊第一次走,要耗費這條邊的權值,問最少耗費是多少

  • 構造s個最小生成樹,用並查集判斷,每個並查集的首領爲這s個點中的一個
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+9;
long long  fa[maxn],flag[maxn];
struct node
{
    long long  s,e,v;
} edge[maxn*100];
long long root(long long a)
{
    if(fa[a]==a)
        return a;
    return fa[a] = root(fa[a]);
}
void join(long long a,long long b)
{
    long long ffa= root(a);
    long long ffb = root(b);
    if(ffa!=ffb)
    {
        if(flag[ffa])
            fa[ffb] = ffa;
        else
            fa[ffa] = ffb;
    }
}
bool cmp(node a,node b)
{
    return a.v<b.v;
}
int main()
{
    long long i,j,m,n,l,t,s,num,ans;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld %lld %lld %lld",&n,&m,&l,&s);
        for(i = 1; i<=n; i++)
        {
            fa[i] = i;
            flag[i] = 0;
        }
        for(i = 0; i<s; i++)
        {
            scanf("%lld",&num);
            flag[num] = 1;
        }
        for(i = 0; i<m; i++)
        {
            scanf("%lld %lld %lld",&edge[i].s,&edge[i].e,&edge[i].v);
        }

        sort(edge,edge+m,cmp);
        ans = 0;
        for(i = 0; i<m; i++)
        {
            long long sta = edge[i].s;
            long long endd = edge[i].e;
            long long val = edge[i].v;
            if(flag[root(sta)]&&flag[root(endd)]||root(sta)==root(endd))
                continue;
            else
            {
                join(sta,endd);
                ans += val+l;
            }
        }
        printf("%lld\n",ans);

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