题解 | #小红送外卖#
小红送外卖
https://www.nowcoder.com/practice/2850d7c941f6494e82ba74bc899eb512
import java.io.*;
import java.util.*;
public class Main {
static class Edge {
int v, w;
Edge(int v, int w) {
this.v = v;
this.w = w;
}
}
static void solve() {
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
List<List<Edge>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = in.nextInt(), v = in.nextInt(), w = in.nextInt();
graph.get(u).add(new Edge(v, w));
graph.get(v).add(new Edge(u, w));
}
long[] dis = new long[n + 1];
Arrays.fill(dis, Long.MAX_VALUE);
dis[1] = 0;
PriorityQueue<long[]> pq = new PriorityQueue<>(Comparator.comparingLong(a -> a[0]));
pq.add(new long[] {0, 1});
while (!pq.isEmpty()) {
long[] cur = pq.poll();
long d = cur[0];
int u = (int) cur[1];
if (d > dis[u]) continue;
for (Edge edge : graph.get(u)) {
int v = edge.v;
int weight = edge.w;
if (dis[u] + weight < dis[v]) {
dis[v] = dis[u] + weight;
pq.add(new long[] {dis[v], v});
}
}
}
long total = 0;
for (int i = 0; i < q; i++) {
int school = in.nextInt();
if (dis[school] != Long.MAX_VALUE) {
total += dis[school] * 2;
}
}
out.println(total);
}
public static void main(String[] args) {
solve();
out.flush();
}
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static class FastReader {
static BufferedReader br;
static StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
String str = "";
while (st == null || !st.hasMoreElements()) {
try {
str = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
st = new StringTokenizer(str);
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
查看3道真题和解析