UVA 11374 - Airport Express(dijstra)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   ar   for   代碼   sp   

UVA 11374 - Airport Express

題目連結

題意:給定一些經濟線,和一些商務線,商務線最多坐一次,每個線有一個時間,問最短時間

思路:從起點,終點各做一次dijstra,然後枚舉商務線,就可以算出總時間,最後求出總時間最少

代碼:

#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;#define INF 0x3f3f3f3fconst int MAXNODE = 505;struct Edge {int u, v, dist;Edge() {}Edge(int u, int v, int dist) {this->u = u;this->v = v;this->dist = dist;}};struct HeapNode {int d, u;HeapNode() {}HeapNode(int d, int u) {this->d = d;this->u = u;}bool operator < (const HeapNode& c) const {return d > c.d;}};struct Dijkstra {int n, m;vector<Edge> edges;vector<int> g[MAXNODE];bool done[MAXNODE];int d[MAXNODE], p[MAXNODE];void init(int tot) {n = tot;for (int i = 0; i < n; i++)g[i].clear();edges.clear();}void add_Edge(int u, int v, int dist) {edges.push_back(Edge(u, v, dist));m = edges.size();g[u].push_back(m - 1);}void print(int s, int e) {//shun xuif (s == e) {printf("%d", e + 1);return;}print(s, edges[p[e]].u);printf(" %d", e + 1);}void print2(int s, int e) {//ni xuif (s == e) {printf("%d\n", e + 1);return;}printf("%d ", e + 1);print2(s, edges[p[e]].u);}void dijkstra(int s) {priority_queue<HeapNode> Q;for (int i = 0; i < n; i++) d[i] = INF;d[s] = 0;memset(done, false, 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.v] > d[u] + e.dist) {d[e.v] = d[u] + e.dist;p[e.v] = g[u][i];Q.push(HeapNode(d[e.v], e.v));}}}}} gao1, gao2;const int M = 2005;int n, s, e;Edge em[M];int main() {int bo = 0;while (~scanf("%d%d%d", &n, &s, &e)) {if (bo) printf("\n");else bo = 1;gao1.init(n);gao2.init(n);s--; e--;int m, x, y, z;scanf("%d", &m);while (m--) {scanf("%d%d%d", &x, &y, &z);x--; y--;gao1.add_Edge(x, y, z);gao1.add_Edge(y, x, z);gao2.add_Edge(x, y, z);gao2.add_Edge(y, x, z);}scanf("%d", &m);for (int i = 0; i < m; i++) {scanf("%d%d%d", &x, &y, &z);x--; y--;em[i * 2] = Edge(x, y, z);em[i * 2 + 1] = Edge(y, x, z);}gao1.dijkstra(s);gao2.dijkstra(e);int ans = gao1.d[e];int ss = e, ee = -1;for (int i = 0; i < 2 * m; i++) {int u = em[i].u, v = em[i].v, dist = em[i].dist;int sum = gao1.d[u] + gao2.d[v] + dist;if (sum < ans) {ans = sum;ss = u;ee = v;}}gao1.print(s, ss);if (ee != -1) {printf(" ");gao2.print2(e, ee);}else printf("\nTicket Not Used\n");if (ee != -1) printf("%d\n", ss + 1);printf("%d\n", ans);}return 0;}


UVA 11374 - Airport Express(dijstra)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.