Ultraviolet A 10246-Asterix and Obelix (Shortest Path)

Source: Internet
Author: User
Ultraviolet A 10246-Asterix and Obelix

Question Link

Question: Given a graph, each vertex has a price and a price. Now there are Q queries. The minimum cost of each query from u to V is calculated in the following way, the cost of the path plus the cost of the maximum cost node on the path

Train of Thought: enumerate the most costly nodes, and then perform Dijkstra. During the process, ignore the points that are more costly than the enumeration points, and then update all the answers, after preprocessing, each query can be completed within O (1 ).

Code:

#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int MAXNODE = 85;const int MAXEDGE = 10005;typedef int Type;const Type INF = 0x3f3f3f3f;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;}};int n, m, q, cost[MAXNODE], ans[MAXNODE][MAXNODE];struct Dijkstra {int n, m;Edge edges[MAXEDGE];int first[MAXNODE];int next[MAXEDGE];bool done[MAXNODE];Type d[MAXNODE];int p[MAXNODE];void init(int n) {this->n = n;memset(first, -1, sizeof(first));m = 0;}void add_Edge(int u, int v, Type dist) {edges[m] = Edge(u, v, dist);next[m] = first[u];first[u] = m++;}void dijkstra(int s) {priority_queue<HeapNode> Q;for (int i = 0; i < n; i++) d[i] = INF;d[s] = 0;p[s] = -1;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 = first[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (cost[e.v] > cost[s]) continue;if (d[e.v] > d[u] + e.dist) {d[e.v] = d[u] + e.dist;p[e.v] = i;Q.push(HeapNode(d[e.v], e.v));}}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {ans[i][j] = min(ans[i][j], d[i] + d[j] + cost[s]);}}}} gao;int main() {int cas = 0;int bo = 0;while (~scanf("%d%d%d", &n, &m, &q) && n) {if (bo) printf("\n");else bo = 1;gao.init(n);for (int i = 0; i < n; i++) scanf("%d", &cost[i]);int u, v, d;while (m--) {scanf("%d%d%d", &u, &v, &d);u--; v--;gao.add_Edge(u, v, d);gao.add_Edge(v, u, d);}memset(ans, INF, sizeof(ans));for (int i = 0; i < n; i++) gao.dijkstra(i);for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)if (ans[i][j] == INF) ans[i][j] = -1;printf("Case #%d\n", ++cas);while (q--) {scanf("%d%d", &u, &v);u--; v--;printf("%d\n", ans[u][v]);}}return 0;}


Ultraviolet A 10246-Asterix and Obelix (Shortest Path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.