uva10537 Dijkstra + Reverse push

Source: Internet
Author: User

21:49:45 2015-03-09

Transfer http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 1478

The problem is that the transportation of goods requires payment of tolls. Enter a village need to pay 1 units of goods, and enter a town is every 20 units of goods will be turned over 1 units (70 to be handed over 4) asked which road to choose to make the least tolls,

This problem we know the end too, then we can push back, for example, we know the last stop we can reverse the previous station, the use of Dijkstra to calculate each point to the end of the optimal toll, and then a greedy to take the smallest result.

1#include <iostream>2#include <algorithm>3#include <string.h>4#include <vector>5#include <queue>6#include <map>7#include <cstdio>8 using namespacestd;9 Const intmaxn= -;Ten Const Long LongINF = 1ll<< -; OnetypedefLong LongLL; A intHash_num (Charc) { -     if(c>='A'&&c<='Z')returnC-'A'; -     Else returnC-'a'+ -; the } - CharHash_char (intc) { -    if(c>=0&&c< -)returnc+'A'; -    Else returnC- -+'a'; + } - intst,ed; + LL D[MAXN]; A BOOLG[MAXN][MAXN],MARK[MAXN]; atll Projud (ll item,intnext) { -     if(next< -)returnitem-(item+ +)/ -; -     returnitem-1; - } -LL Jud (intu) { -    if(U >= -)returnd[u]+1; inLL v =D[u]; -LL ans=0; toans=v; +         while(true){ -LL d=v/ -; thev=v% -; *ans+=D; $v+=D;Panax Notoginseng           if(d==0) Break; -        } the        if(v) ans++; +        returnans; A } thell forward (ll item,intnext) { +   if(Next < -)returnItem-(Item + +) / -; -   returnItem-1; $ } $ structhead{ -LL D;intu; -    BOOL operator< (ConstHead &r)Const { the      returnD>R.D; -    }WuyiHead (LL dd =0,intUU =0){ theD=DD; U =UU; -    } Wu }; - voidprint (LL p) { About      intNod= the; $memset (Mark,false,sizeof(Mark)); -        for(inti =0; I<nod; i++) d[i]=INF; -d[ed]=p; -Priority_queueQ; A Q.push (Head (p,ed)); +        while(!Q.empty ()) { theHead x=Q.top (); Q.pop (); -            if(Mark[x.u])Continue; $mark[x.u]=true; the             for(intI=0; i<nod; ++i) { theLL PE =Jud (x.u); the              if(mark[i]==false&&G[i][x.u]&&pe<D[i]) { theD[i] =PE; - Q.push (Head (d[i],i)); in              } the            } the       } About  the     intu=St; theprintf"%lld\n", d[st]); theprintf"%c", Hash_char (U)); +  -LL item =D[st]; the    while(U! =ed) {Bayi     intNext; the      for(Next =0; Next < nod; next++)if(G[u][next] && forward (item, next) >= D[next]) Break; theitem =D[next]; -printf"-%c", Hash_char (next)); -U =Next; the   } theprintf"\ n"); the } the intMain () - { the     intn,cas=0; the     Chars1[9],s2[9]; the 94      while(SCANF ("%d", &n) = =1&&n!=-1){ thememset (G,false,sizeof(G)); the  the          for(intI=0; i<n; ++i) {98scanf"%s%s", S1,S2); About              intU = Hash_num (s1[0]); -              intv = hash_num (s2[0]);101             if(U==V)Continue;102g[u][v]=g[v][u]=true;103 104  the         }106 LL p;107scanf"%lld%s%s",&p,s1,s2);108st = Hash_num (s1[0]); ed = Hash_num (s2[0]);109printf"Case %d:\n",++CAs); the print (p);111     } the     return 0;113}

uva10537 Dijkstra + Reverse push

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.