UVA 10537 toll! Revisited (priority queue optimization Dijstra and deformation)

Source: Internet
Author: User

toll! Revisited

Roughly test instructions: There are two kinds of nodes, one is capital letters and one is lowercase. First enter the M-bar, when the lowercase letter is required to pay a unit of tolls, when the capital letter, to pay the current financial 1/20 of the tolls. Ask at least how many items you need at the beginning to reach the finish line with K items. The smallest of the output dictionaries when there are multiple paths that match a condition.


Idea: The weight of the known end point can then be pushed forward from the end point. Find the shortest path to the starting point, and then print the path in dictionary order.

It is difficult to deal with: the weights of the precursor nodes are calculated when pushing forward. The calculation of a single equation can be, the main is not divisible by the case.

When calculating the pre-node dis value, the Benquan value of (I,J) is recorded simultaneously, which is the basis of the print path.


#include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector># Include <math.h> #include <string.h> #include <stack> #include <queue> #define LL long long#    Define _ll __int64using namespace Std;const int INF = 0x3f3f3f3f;const int MAXM = 1010;const int MAXN = 60;struct node{    int v,w; int next;} Edge[maxm];int m;int Pre[maxn],cnt;int start,end;    LL dis[maxn],p;int vis[maxn];vector <int> ans;void init () {cnt = 0;    memset (pre,-1,sizeof (pre)); for (int i = 0; i < MAXM; i++) edge[i].w = 0;}    void Add (int u, int v) {edge[cnt].v = v;    Edge[cnt].next = Pre[u]; Pre[u] = cnt++;} void Dijstra () {priority_queue <pair<ll,int>, vector<pair<ll,int>;, greater<pair<ll,int& Gt    > > que;    while (!que.empty ()) Que.pop ();    memset (dis,inf,sizeof (dis));    memset (vis,0,sizeof (VIS));    Dis[end] = p;    Que.push (Make_pair (dis[end],end));       while (!que.empty ()) { int u = que.top (). Second;        Que.pop ();        if (Vis[u]) continue;        Vis[u] = 1;            for (int i = pre[u]; i =-1; i = edge[i].next)//slack adjacent node {if (VIS[EDGE[I].V]) continue;            int v = EDGE[I].V;                if (U < 26) {//Compute the weights of the precursor nodes, determine if the division is divisible, if not divisible, try adding 1 to continue judging if (dis[u]%19 = = 0)                        {if (Dis[v] > dis[u]/19*20) {dis[v] = dis[u]/19*20;                        EDGE[I].W = EDGE[I^1].W = Dis[v]-dis[u];                    Que.push (Make_pair (dis[v],v)); }} else if ((dis[u]+1)%19) {if (Dis[v] > (dis[u]+1) * *                        0/19) {Dis[v] = (dis[u]+1) *20/19;                        EDGE[I].W = EDGE[I^1].W = Dis[v]-dis[u];                    Que.push (Make_pair (dis[v],v));   }} else             {if (Dis[v] > (dis[u]+1) *20/19-1) {Dis[v] =                        (dis[u]+1) *20/19-1;                        EDGE[I].W = EDGE[I^1].W = Dis[v]-dis[u];                    Que.push (Make_pair (dis[v],v));                    }}} else {if (Dis[v] > dis[u]+1) {                    DIS[V] = dis[u]+1;                    EDGE[I].W = EDGE[I^1].W = 1;                Que.push (Make_pair (dis[v],v));    }}}}}void solve () {ans.clear ();    int now;    now = start;    Ans.push_back (now);        while (now! = end) {int TMP = 1<<6;            for (int i = Pre[now]; i =-1; i = edge[i].next) {int v = EDGE[I].V;             Because the output dictionary order is minimal, the smallest v if (Dis[now]-dis[v] = = EDGE[I].W && v < tmp) is found to satisfy Dis[now]-dis[v] = = EDGE[I].W            {tmp = V;        }} ans.push_back (TMP);    now = tmp;    } printf ("%c", ans[0]+ ' A ');    for (int i = 1; i < (int) ans.size (); i++) printf ("-%c", ans[i]+ ' A '); printf ("\ n");}    int main () {int item = 1;    Char t1,t2;        while (~SCANF ("%d", &m)) {if (M = =-1) break;        Init ();        GetChar ();            for (int i = 0; i < m; i++) {scanf ("%c%c", &t1,&t2);            GetChar ();            Add (t1-' a ', t2-' a ');        Add (t2-' a ', t1-' a ');        } scanf ("%lld%c%c", &p,&t1,&t2);        Start = T1-' A ';        end = T2-' A ';        Dijstra ();        printf ("Case%d:\n", item++);        printf ("%lld\n", Dis[start]);    Solve (); } return 0;}


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.