const int MAXV = 600; const int MAXE = 1100; //堆优化的Dijkstra struct Edge { int from, to, dist; }; struct HeapNode { int d, u; bool operator < (const HeapNode& rhs) const { return d > rhs.d; } }; int ans; vector<int> vt; struct Dijkstra { int n, m; vector<Edge> edges; vector<int> G[MAXV]; bool done[MAXV]; // 是否已永久标号 int d[MAXV]; // s到各个点的距离 int p[MAXV]; // 最短路中的上一条弧 void init(int n) { this->n = n; for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int dist) { edges.push_back((Edge) { from, to, dist }); m = edges.size(); G[from].push_back(m - 1); } void dijkstra(int s) { priority_queue<HeapNode> Q; for(int i = 0; i < n; i++) d[i] = INF; d[s] = 0; memset(done, 0, sizeof(done)); Q.push((HeapNode) { 0, s }); while(!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if(done[u]) continue; done[u] = true; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; p[e.to] = G[u][i]; Q.push((HeapNode) { d[e.to], e.to }); } } } } void display1(int u) { if (d[u] == 0) goto end; display1(edges[p[u]].from); end:; vt.push_back(u + 1); } void display2(int u) { vt.push_back(u + 1); if (d[u] == 0) return; display2(edges[p[u]].from); } } sd, ed; void display() { int len = vt.size(); REP(i, len) { if (i) putchar(‘ ‘); printf("%d", vt[i]); } puts(""); } int main() { // freopen("in.txt", "r", stdin); int N, S, E, M, K, a, b, c, t, u, v; int kase = 0; while (~RIII(N, S, E)) { if(kase != 0) printf("\n"); vt.clear(); S--; E--; ans = INF; sd.init(N); ed.init(N); RI(M); REP(i, M) { RIII(a, b, c); a--; b--; sd.AddEdge(a, b, c); sd.AddEdge(b, a, c); ed.AddEdge(a, b, c); ed.AddEdge(b, a, c); } sd.dijkstra(S); ed.dijkstra(E); RI(K); REP(i, K) { RIII(a, b, c); a--; b--; if ((t = sd.d[a] + ed.d[b] + c) < ans) { u = a; v = b; ans = t; } else if ((t = sd.d[b] + ed.d[a] + c) < ans) { u = b; v = a; ans = t; } } if (sd.d[E] < ans) { ans = sd.d[E]; sd.display1(E); display(); puts("Ticket Not Used"); } else { sd.display1(u); ed.display2(v); display(); WI(u + 1); } WI(ans); kase = 1; } return 0; }
Airport Express,布布扣,bubuko.com
原文:http://blog.csdn.net/wty__/article/details/22313483