POJ 3159 Candies (Dijstra optimized non-vector notation)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3159

Test instructions: give n personal candy, give the M group of data, each group of data contains a,b,c three number, meaning a candy number than B less than the number of C, that is, B candy number-a candy number <= C.

The last to ask N than 1 more than the number of sweets.

Can be from the condition of the

B-a<=c and B<=a+c finally to achieve this condition is to be b>a+c when the b=a+c can

So it's almost the shortest way. There are some optimizations in this question, such as

if (Vis[u]) continue; This avoids duplicate lookups of U-Points

#include <iostream> #include <string> #include <cstring> #include <vector> #include <queue > #include <cstdio> #define INF 0x3f3f3f3fusing namespace std;const int m = 2e5;int N, m, A, B, C, dis[30010]; struct TnT {int u, V, W, next;}    t[m];struct qnode{int V, c;    Qnode (int v, int c): V (v), C (c) {} BOOL operator < (const qnode &AMP;R) const{return c > r.c;    }};int head[30010], e;void Add (int u, int v, int w) {t[e].v = v;    T[E].W = W;    T[e].next = Head[u]; Head[u] = e++;}    BOOL Vis[m];void dij (int s) {priority_queue<qnode>q;    Memset (Vis, false, sizeof (VIS));    Q.push (Qnode (s, 0));    Dis[s] = 0;        while (!q.empty ()) {int u = q.top (). V;        Q.pop ();        if (Vis[u]) continue;        Vis[u] = true;            for (int i = head[u]; i =-1; i = t[i].next) {int v = t[i].v, w = T[I].W; if (!vis[v] && dis[v] > Dis[u] + W) {Dis[v] = dIs[u] + W;            Q.push (Qnode (V, Dis[v]));        }}}}int Main () {while (scanf ("%d%d", &n, &m)! = EOF) {e = 0;            for (int i = 1; I <= n; i++) {dis[i] = inf;        Head[i] =-1;            } for (int i = 1; I <= m; i++) {scanf ("%d%d%d", &a, &b, &c);        Add (A, B, c);        } dij (1);    printf ("%d\n", Dis[n]); } return 0;}

Poj 3159 Candies (Dijstra optimized non-vector notation)

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.