UVA 10801 Lift Hopping elevator transfer (shortest way, deformation)

Source: Internet
Author: User

Test instructions: There is a n<6 elevator, give each elevator can stop some of the specific floors, request from 0 floor to reach the K-level out, each transfer takes 60 seconds, each elevator through each floor time is different, specific by the number of floors * elevator speed to calculate. Q How many seconds to reach the K-level (K can be 0)?

Idea: Dijkstra add some special treatment on the line. The first thing to consider is how to build the diagram:

(1) Each layer as a point. But the specific path can have a variety of rights, such as from 2->5 can sit 1th elevator 10s, but sit 2nd only need 5s, so there are heavy edges.

(2) When k=0, it is not time-consuming.

(3) There are many ways to reach the same floor and the same weight, then from the floor to another floor has a variety of options, sometimes can not change elevators, sometimes need to change. For example, reached the 5 floor has 2 paths, the right is 5, but two different elevators 1 and 2, at this time there are other elevators from 5 to 7 floors, one of which is still elevator 1, if the elevator 1 does not need to transfer time, sitting other elevators will be. So you have to record an elevator number that is equal to a point weight.

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=1010;7 inttake[6];8 Chars[ +];9vector<int> lift[6], vect[ -];Ten intCost[n], vis[n]; One structnode A { -     int  from, to, lift, cost; - node () {}; theNodeint  from,intTo,intLiftintCost): from( from), to, lift (lift), cost (cost) {}; -}edge[100000]; - intedge_cnt; -  + voidAdd_node (int  from,intTo,intLiftintCost ) - { +Edge[edge_cnt]=node ( from, to,lift,cost); Avect[ from].push_back (edge_cnt++); at } -  - voidBuild_graph (intN//re-build the map - { -      for(intI=0; i<n; i++) -     { in          for(intj=0; J<lift[i].size (); J + +) -         { to              for(intt=j+1; T<lift[i].size (); t++) +             { -                 intA=Lift[i][j]; the                 intb=Lift[i][t]; *Add_node (A,b,i, (b-a) *take[i]); $Add_node (B,a,i, (b-a) *take[i]);Panax Notoginseng             } -         } the     } + } A  the intDijkstraintSinte) + { -vector<int> flo[101]; $memset (Cost,0x7f,sizeof(cost)); $memset (Vis,0,sizeof(Vis)); -  -priority_queue<pii,vector<pii>,greater<pii> >que; theQue.push (Make_pair (0, s)); -cost[s]=0;Wuyi      while(!que.empty ()) the     { -         intx=que.top (). Second;que.pop (); Wu         if(Vis[x])Continue; -  Aboutvis[x]=1; $          for(intI=0; I<vect[x].size (); i++) -         { -Node e=Edge[vect[x][i]]; -             intext= -; A  +              for(intj=0; J<flo[e. from].size (); J + +)//If there is a match, there is no extra time the                 if(Flo[e. from][j]==e.lift) ext=0; -  $             if(Cost[e.to]>=cost[e. from]+ext+e.cost) the             { the                 if(Cost[e.to]>cost[e. from]+ext+e.cost) flo[e.to].clear (); the Flo[e.to].push_back (e.lift); the  -Cost[e.to]= cost[e. from] +ext +E.cost; in Que.push (Make_pair (cost[e.to], e.to)); the             } the         } About     } the     returnCost[e]; the } the  + intMain () - { theFreopen ("Input.txt","R", stdin);Bayi     intN, K; the      while(~SCANF ("%d%d",&n,&k)) the     { -          for(intI=0; i<n; i++) lift[i].clear (); -          for(intI=0; i<101; i++) vect[i].clear (); thememset (s),0,sizeof(s)); the  the          for(intI=0; i<n; i++) scanf ("%d",&take[i]); the GetChar (); -          for(intI=0; i<n; i++) the         { the lift[i].clear (); the gets (s);94             intp=0; the              while(s[p]!=' /') the             { the                 if(s[p]==' ') p++;98                 inttmp=0; About                  while(s[p]!=' '&&s[p]!=' /') tmp=tmp*Ten+ (s[p++]-'0'); - Lift[i].push_back (TMP);101             }102         }103 build_graph (n);104         intAns=dijkstra (0, k); the         if(!k) puts ("0");106         Else if(Ans==inf) puts ("Impossible");107         Elseprintf"%d\n", ans- - ) ;108     }109     return 0; the}
AC Code

UVA 10801 Lift Hopping elevator transfer (shortest way, deformation)

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.