UVA 10537 - The Toll! Revisited(dijstra擴充)

來源:互聯網
上載者:User

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

UVA 10537 - The Toll! Revisited

題目連結

題意:給定一個無向圖,大寫字母是城市,小寫字母是村莊,經過城市交過路費為當前貨物的%5,路過村莊固定交1,給定起點終點和到目標地點要剩下的貨物,問最少要帶多少貨物上路,並輸出路徑,如果有多種方案,要求字典序最小

思路:dijstra的逆向運用,d數組含義變成到該結點至少需要這麼多貨物,然後反向建圖,從終點向起點反向做一遍

這題被坑了。。並不是輸出的城市才存在,比如下面這組範例
0
1 A A
應該輸出
1
A

代碼:

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <cmath>using namespace std;const int MAXNODE = 105;typedef long long Type;const Type INF = (1LL<<61);struct Edge {int u, v;Type dist;Edge() {}Edge(int u, int v, Type dist) {this->u = u;this->v = v;this->dist = dist;}};struct HeapNode {Type d;int u;HeapNode() {}HeapNode(Type d, int u) {this->d = d;this->u = u;}bool operator < (const HeapNode& c) const {return d > c.d;}};char to[255];struct Dijkstra {int n, m;vector<Edge> edges;vector<int> g[MAXNODE];bool done[MAXNODE];Type d[MAXNODE];int 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, Type dist) {edges.push_back(Edge(u, v, dist));m = edges.size();g[u].push_back(m - 1);}void print(int e) {if (p[e] == -1) {printf("%c\n", to[e]);return;}printf("%c-", to[e]);print(edges[p[e]].u);}void dijkstra(Type start, int s) {priority_queue<HeapNode> Q;for (int i = 0; i < n; i++) d[i] = INF;d[s] = start;p[s] = -1;memset(done, false, sizeof(done));Q.push(HeapNode(start, 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]];Type need;if (e.dist) need = (Type)ceil(d[u] * 1.0 / 19 * 20);else need = d[u] + 1;if (d[e.v] > need || (d[e.v] == need && to[u] < to[edges[p[e.v]].u])) {d[e.v] = need;p[e.v] = g[u][i];Q.push(HeapNode(d[e.v], e.v));}}}}} gao;typedef long long ll;int n, m, vis[255];int main() {int cas = 0;for (int i = 0; i < 26; i++) {vis['A' + i] = i;to[i] = 'A' + i;}for (int i = 0; i < 26; i++) {vis['a' + i] = i + 26;to[i + 26] = 'a' + i;}while (~scanf("%d", &m) && m != -1) {gao.init(52);char a[2], b[2];int u, v;while (m--) {scanf("%s%s", a, b);u = vis[a[0]], v = vis[b[0]];gao.add_Edge(u, v, a[0] < 'a');gao.add_Edge(v, u, b[0] < 'a');}ll need;scanf("%lld%s%s", &need, a, b);u = vis[a[0]]; v = vis[b[0]];gao.dijkstra(need, v);printf("Case %d:\n", ++cas);printf("%lld\n", gao.d[u]);gao.print(u);}return 0;}


UVA 10537 - The Toll! Revisited(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.