-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCFE.cpp
More file actions
78 lines (75 loc) · 1.2 KB
/
CFE.cpp
File metadata and controls
78 lines (75 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n,m,a,b,c;
vector<int> bfs(vector<ll> *adj,int src)
{
vector<int> dist;
queue<int> q;
for(int i=0;i<=n;i++)
{
dist.push_back(INT_MAX);
}
q.push(src);
dist[src]=0;
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto nbr: adj[node])
{
if(dist[nbr]==INT_MAX)
{
q.push(nbr);
dist[nbr]=dist[node]+1;
}
}
}
return dist;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("otpt.txt","w",stdout);
#endif
int t;
cin>>t;
while(t--)
{
cin>>n>>m>>a>>b>>c;
vector<ll> p;
p.push_back(0);
for(int i=1;i<=m;i++)
{
ll x;
cin>>x;
p.push_back(x);
}
sort(p.begin(),p.end());
for(int i=1;i<=m;i++)
p[i]+=p[i-1];
vector<ll> adj[n+1];
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> da=bfs(adj,a);
vector<int> db=bfs(adj,b);
vector<int> dc=bfs(adj,c);
ll ans=LLONG_MAX;
for(int i=1;i<=n;i++)
{
ll dbv=p[db[i]];
if(db[i]+da[i]+dc[i]>m)
continue;
ll dtv=p[db[i]+da[i]+dc[i]];
ans=min(ans,dbv+dtv);
}
cout<<ans<<endl;
}
}