|
| 1 | +#include <algorithm> |
| 2 | +#include <cstring> |
| 3 | +#include <iostream> |
| 4 | +#include <queue> |
| 5 | +#include <tuple> |
| 6 | +#include <utility> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +typedef pair<int, int> data_t; |
| 12 | + |
| 13 | +int T, n, m, t, s, g, h; |
| 14 | + |
| 15 | +vector<int> ret, dest_candidates; |
| 16 | +vector<pair<int, int>> adj[2001]; |
| 17 | + |
| 18 | +void dijkstra(int start, int *dist) { |
| 19 | + priority_queue<data_t, vector<data_t>, greater<data_t>> q; |
| 20 | + q.push(make_pair(0, start)); |
| 21 | + |
| 22 | + while (!q.empty()) { |
| 23 | + int here, cost; |
| 24 | + cost = q.top().first; |
| 25 | + here = q.top().second; |
| 26 | + q.pop(); |
| 27 | + |
| 28 | + if (dist[here] < cost) |
| 29 | + continue; |
| 30 | + |
| 31 | + for (int i = 0; i < adj[here].size(); ++i) { |
| 32 | + int there, nextCost; |
| 33 | + there = adj[here][i].first; |
| 34 | + nextCost = adj[here][i].second; |
| 35 | + |
| 36 | + if (cost + nextCost < dist[there]) { |
| 37 | + dist[there] = cost + nextCost; |
| 38 | + q.push(make_pair(cost + nextCost, there)); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void solve() { |
| 45 | + int dist[2001]; |
| 46 | + fill_n(dist, 2001, 50000 * 2 * 1000 + 2); |
| 47 | + dist[s] = 0; |
| 48 | + |
| 49 | + dijkstra(s, dist); |
| 50 | + |
| 51 | + for (int i = 0; i < dest_candidates.size(); i++) { |
| 52 | + int dest = dest_candidates[i]; |
| 53 | + |
| 54 | + if (dist[dest] % 2 == 1) |
| 55 | + ret.push_back(dest); |
| 56 | + } |
| 57 | + |
| 58 | + sort(ret.begin(), ret.end()); |
| 59 | +} |
| 60 | + |
| 61 | +int main() { |
| 62 | + ios::sync_with_stdio(false); |
| 63 | + cin.tie(NULL); |
| 64 | + |
| 65 | + cin >> T; |
| 66 | + for (int i = 0; i < T; ++i) { |
| 67 | + cin >> n >> m >> t; |
| 68 | + cin >> s >> g >> h; |
| 69 | + for (int j = 0; j < m; ++j) { |
| 70 | + int a, d, b; |
| 71 | + cin >> a >> b >> d; |
| 72 | + |
| 73 | + d *= 2; |
| 74 | + if ((a == g && b == h) || (b == g && a == h)) { |
| 75 | + d -= 1; |
| 76 | + } |
| 77 | + adj[a].push_back(make_pair(b, d)); |
| 78 | + adj[b].push_back(make_pair(a, d)); |
| 79 | + } |
| 80 | + |
| 81 | + for (int j = 0; j < t; ++j) { |
| 82 | + int x; |
| 83 | + cin >> x; |
| 84 | + dest_candidates.push_back(x); |
| 85 | + } |
| 86 | + |
| 87 | + solve(); |
| 88 | + |
| 89 | + // print |
| 90 | + for (int j = 0; j < ret.size(); ++j) { |
| 91 | + cout << ret[j] << " "; |
| 92 | + } |
| 93 | + cout << endl; |
| 94 | + |
| 95 | + // reset |
| 96 | + dest_candidates.clear(); |
| 97 | + ret.clear(); |
| 98 | + for (int i = 0; i < 2001; i++) { |
| 99 | + adj[i].clear(); |
| 100 | + } |
| 101 | + } |
| 102 | + return 0; |
| 103 | +} |
0 commit comments