Nature Reserve
Kattis - naturereserve
In a Nature Reserve and Wildlife Park, there are N N N environmental monitoring stations to monitor temperature, atmospheric pressure, humidity, fire, water quality, etc. Each station, labeled from 1 1 1 to N N N, 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 L L L bytes) to all environmental monitoring stations. The program is initially installed directly to S S S 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 E i j {E}_{ij} Eij energy units to activate the communication channel between station i i i and station j j j. 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 S S S 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
20
20
20. 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 N N N, the number of two-way communication channels M M M, the size of the program L L L (in bytes), and the number of initial stations S S S. The constraints are 1 ≤ S ≤ N ≤ 10 4 1 \leq S \leq N \leq {10}^{4} 1≤S≤N≤104, 1 ≤ M ≤ 10 6 1 \leq M \leq {10}^{6} 1≤M≤106, M ≤ N ( N − 1 ) / 2 M \leq N(N-1)/2 M≤N(N−1)/2, and 1 ≤ L ≤ 10 6 1 \leq L \leq {10}^{6} 1≤L≤106.
The second line contains S S S positive integer representing the initial S S S stations.
Each of the following M M M lines contains three positive integers i , j i, j i,j and E i j {E}_{ij} Eij to denote that there is a two-way communicataion channel between station i i i and station j j j, and it takes E i j {E}_{ij} Eij energy units to activate this channel ( E i j ≤ 10 6 ) ({E}_{ij} \leq {10}^{6}) (Eij≤106).
Output
For each data set, output the minimum energy units required to send the Smart Data Analysis program to all stations from the initial
S
S
S 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;
}