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
| #include<bits/stdc++.h> #define ll long long #define INF LLONG_MAX using namespace std; int t, n, m, k, u, v, p; ll w, cost; vector<int> printers; vector<ll> weights1, weightsN; vector<vector<pair<int, ll>>> graph; struct CompareSecond { bool operator()(const pair<int, ll>& left, const pair<int, ll>& right) const { return left.second > right.second; } }; void dijkstra(int st, vector<ll>& dis) { priority_queue<pair<int, ll>, vector<pair<int, ll>>, CompareSecond> pq; vector<bool> visited(n + 1, false); dis[st] = 0; pq.emplace(st, 0); while(!pq.empty()) { u = pq.top().first; pq.pop(); visited[u] = true; for(pair<int, ll> p0: graph[u]) { if(dis[p0.first] > dis[u] + p0.second && !visited[p0.first]) { dis[p0.first] = dis[u] + p0.second; pq.emplace(p0.first, dis[p0.first]); } } } } int main() { scanf("%d", &t); while(t--) { scanf("%d%d%d", &n, &m, &k); printers.clear(); weights1.assign(n + 1, INF); weightsN.assign(n + 1, INF); graph.assign(n + 1, vector<pair<int, ll>>()); for(int _ = 0; _ < k; _++) { scanf("%d", &p); printers.emplace_back(p); } for(int _ = 0; _ < m; _++) { scanf("%d%d%lld", &u, &v, &w); graph[u].emplace_back(make_pair(v, w)); graph[v].emplace_back(make_pair(u, w)); } dijkstra(1, weights1); dijkstra(n, weightsN); cost = INF; for(int i = 0; i < k; i++) { if(weights1[printers[i]] != INF && weightsN[printers[i]] != INF) cost = min(cost, weights1[printers[i]] + weightsN[printers[i]]); } printf("%lld\n", cost == INF ? -1 : cost); } return 0; }
|