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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
| #include <bits/stdc++.h>
using namespace std;
#define USE_CPPIO() ios_base::sync_with_stdio(0); cin.tie(0)
#define INF 0x3f3f3f3f
#define NINF 0xc0c0c0c0
#define maxn 505
struct edge {
int u, v, dist;
};
struct node {
int d, u;
bool operator < (const node &r) const {
return d > r.d;
}
};
vector<edge> ve;
vector<int> g[maxn], path1[maxn], path2[maxn];
bool done[maxn];
int d[maxn], p[maxn], d1[maxn], d2[maxn];
int ca = 0, n, s, e, m, k;
void dijkstra(int s, int *dist, vector<int> *paths) {
priority_queue<node> pq;
for(int i = 0; i < n; ++i) d[i] = INF;
d[s] = 0;
memset(done, 0, sizeof(done));
pq.push({0, s});
while(!pq.empty()) {
node x = pq.top();
pq.pop();
if(done[x.u]) continue;
done[x.u] = true;
for(int i = 0; i < g[x.u].size(); ++i) {
edge &e = ve[g[x.u][i]];
if(d[e.v] > d[x.u] + e.dist) {
d[e.v] = d[x.u] + e.dist;
p[e.v] = g[x.u][i];
pq.push({d[e.v], e.v});
}
}
}
for(int i = 0; i < n; ++i) {
dist[i] = d[i];
paths[i].clear();
int t = i;
paths[i].push_back(t);
while(t != s) {
paths[i].push_back(ve[p[t]].u);
t = ve[p[t]].u;
}
reverse(paths[i].begin(), paths[i].end());
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
USE_CPPIO();
while(cin >> n >> s >> e >> m) {
for(int i = 0; i < n; ++i) g[i].clear();
ve.clear();
--s;
--e;
for(int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
ve.push_back({a, b, c});
g[a].push_back(ve.size()-1);
ve.push_back({b, a, c});
g[b].push_back(ve.size()-1);
}
dijkstra(s, d1, path1);
dijkstra(e, d2, path2);
int ans = d1[e];
vector<int> pp = path1[e];
int midpoint = -1;
cin >> k;
for(int i = 0; i < k; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a;
--b;
for(int j = 0; j < 2; ++j) {
if(d1[a] + d2[b] + c < ans) {
ans = d1[a] + d2[b] + c;
pp = path1[a];
for(int p = path2[b].size() - 1; p >= 0; --p)
pp.push_back(path2[b][p]);
midpoint = a;
}
swap(a, b);
}
}
if(ca++ != 0) cout << endl;
for(int i = 0; i < pp.size() - 1; ++i) cout << pp[i] + 1 << " ";
cout << e + 1 << endl;
if(midpoint == -1) cout << "Ticket Not Used\n";
else cout << midpoint + 1 << endl;
cout << ans << endl;
}
return 0;
}
|