#include <cstdio> #include <queue> #include <algorithm> using namespace std;
struct edge{ int to, w; edge* next; edge() { to = -1; w = -1; next = NULL; } edge(int _t, int _w) :to(_t), w(_w) { next = NULL; } };
const int maxN = 505; const int inf = 1e5;
class linkedList { private: edge* head; int size; public: linkedList() { head = NULL; size = 0; } ~linkedList() { edge* temp = head; while (temp != NULL) { edge* p = temp; temp = temp->next; p = NULL; delete p; } delete temp; head = NULL; size = 0; } edge* getFirst() { return head; } void add(int to, int w) { edge* node = new edge(to, w); if (head == NULL) { head = node; return; } edge* p = head; edge* pp = NULL; while (p != NULL) { pp = p; p = p->next; } pp->next = node; size++; } };
int dis1[maxN], dis2[maxN];
class graph { private: linkedList arr[maxN]; int size; int pre1[maxN]; int pre2[maxN]; public: graph() { size = 0; } graph(int n) :size(n) { } ~graph() { } void add(int x, int y, int z) { arr[x].add(y, z); arr[y].add(x, z); } void dijkstra(int s, bool sta) { priority_queue<pair<int, int>> q;
bool reach[maxN] = { false }; for (int i = 1; i <= size; i++) { if (sta) dis1[i] = inf; if (!sta) dis2[i] = inf; } if (sta) dis1[s] = 0; if (!sta) dis2[s] = 0;
q.push(make_pair(0, s));
while (!q.empty()) { int x = q.top().second; q.pop(); if (reach[x]) continue; reach[x] = true; for (edge* temp = arr[x].getFirst(); temp != NULL; temp = temp->next) { int dest = temp->to; int weight = temp->w; if (sta) { if (dis1[dest] > dis1[x] + weight) { dis1[dest] = dis1[x] + weight; pre1[dest] = x; q.push(make_pair(-dis1[dest], dest)); } } if (!sta) { if (dis2[dest] > dis2[x] + weight) { dis2[dest] = dis2[x] + weight; pre2[dest] = x; q.push(make_pair(-dis2[dest], dest)); } } } } } void output1(int s, int cur) { if (s != cur) output1(s, pre1[cur]); if (s == cur) printf("%d", cur); else printf(" %d", cur); } void output2(int e, int cur) { while (cur != e) { printf(" %d", cur); cur = pre2[cur]; } printf(" %d", cur); } };
int main() { int n, s, e; bool firstOutput = true; while (scanf("%d %d %d", &n, &s, &e) != EOF) { if (!firstOutput) printf("\n");
int m; scanf("%d", &m); graph G(n); for (int i = 0; i < m; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); G.add(x, y, z); } G.dijkstra(s, true); G.dijkstra(e, false); int k; scanf("%d", &k); int ans = inf; int d1, d2; for (int i = 0; i < k; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (ans > min(dis1[x] + dis2[y] + z, dis2[x] + dis1[y] + z)) { ans = min(dis1[x] + dis2[y] + z, dis2[x] + dis1[y] + z); if (dis1[x] + dis2[y] > dis1[y] + dis2[x]) swap(x, y); d2 = x; d1 = y; } }
if (firstOutput) firstOutput = false;
if (ans < dis1[e]) { G.output1(s, d2); G.output2(e, d1); printf("\n%d\n", d2); } else { ans = dis1[e]; G.output1(s, e); printf("\nTicket Not Used\n"); } printf("%d\n", ans); } return 0; }
|