加一个点0 和 n + 1,0到所有发射器建边, 所有接收器到n + 1建边, 最后跑一遍dijisktra, 如果能到n + 1就是最短时间,不然输出-1。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define lb long double
const ll INF = 1e18 + 7;
const int maxn = 5e5 + 10;
const ll mod = 998244353;
int n, m, s, t, cnt;
int head[maxn], vis[maxn];
ll dis[maxn];
struct node{
int to;
int next;
int quan;
}edge[maxn << 1];
void add(int u, int v, int w){
edge[cnt].to = v;
edge[cnt].quan = w;
edge[cnt].next = head[u];
head[u] = cnt ++;
}
struct nod{
int u;
ll d;
bool operator< (const nod& xgd) const{
return d > xgd.d;
}
};
void diji(){
dis[0] = 0;
priority_queue<nod> que;
que.push(nod{0, 0});
while(!que.empty()){
nod cur = que.top();
que.pop();
int u = cur.u;
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u] ; i != -1 ; i = edge[i].next){
int v = edge[i].to;
if(!vis[v] && dis[v] > dis[u] + edge[i].quan){
dis[v] = edge[i].quan + dis[u];
que.push(nod{v, dis[v]});
}
}
}
}
int main()
{
int u, v, w;
scanf("%d %d %d %d", &n, &m, &s, &t);
for(int i = 0 ; i <= n + 1 ; ++ i) head[i] = -1, dis[i] = INF;
for(int i = 1 ; i <= s ; ++ i){
scanf("%d", &v);
add(0, v, 0);
}
for(int i = 1 ; i <= t ; ++ i){
scanf("%d", &u);
add(u, n + 1, 0);
}
for(int i = 1 ; i <= m ; ++ i){
scanf("%d %d %d", &u, &v, &w);
add(u, v, w); add(v, u, w);
}
diji();
if(dis[n + 1] < INF) printf("%lld", dis[n + 1]);
else printf("-1");
return 0;
}