Ivan ' s cartime limit:1.5 second
Memory limit:64 mbthe world are in danger! Awful earthquakes is detected all through the world. Houses is destroyed, rivers overflow the banks, it's almost impossible to move from one city to another. Some roads is still useful, but even they became too steep because of soil movements. Fortunately, engineer Ivan have a car, which can go well uphill and downhill. But there is different gear-modes for movement up and off, so during the driving that you had to change gear-modes all the T Ime. Also Engineer Ivan has a good friend ––geologist Orlov. Together They is able to invent a plan for world saving. But, unfortunately, the geologist Orlov lives in the another town. Ivan wants to save the world, but Gear-box in his car started to wear out, so he doesn ' t know, how long he'll be able to Use it. Please help Ivan to save the world. Find a route to the Orlov's town, such that Ivan would have to change gear-modes as few times as possible. In the beginning of the the-the-can turn on any of gear-mOdes and you don't have the to count this action as a changing of gear-mode. Inputthere is positive integer numbers
Nand
mIn the first line, the number of towns and roads between them respectively (2≤
N≤10 000; 1≤
m≤100 000). Next
mLines contain-numbers each-numbers of towns, which is connected by road. Moreover, the first is the town, which are situated below, from which to should go uphill by this road. Every road can be used for traveling in any of the directions. There is at the most one road between any and cities. The last line there is numbers of the cities, in which Ivan and geologist Orlov live, respectively. Although the majority of roads were destroyed, Ivan knows exactly, that the the by geologist Orlov ' s city exists. Outputoutput the smallest number of gear-modes changes on the the-the-the-Orlov ' s city. Samples
input |
Output |
3 21 23) 21 3 |
1 |
3 31 22 33 11 3 |
0 |
problem Author:Grigoriy Nazarov (prepared by Bulat Zaynullin)"Analysis" test instructions did not say, should be able to understand, I said my approach, may be a bit cumbersome. first with the vector storage edge, up storage, down, and then BFS, with the priority queue in the state, each time from the queue to take the minimum number of operations, due to the complexity and memory problems, need to reduce the branch, when walking to a point, see the current number of operations with the previous size, if the small put into the queue, If the same is true, the last direction of the point is the same as this one, and if it is not the same and the direction of this point is not present, then put in the queue. The beginning has been wrong, changed to change to the text 11, first MLE, changed a bit, WA, and then change TLE, are quick to give up, and later added a vis, too, later encountered this situation must not give up.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 0x3f3f3f3f#defineMoD 1000000007typedefLong Longll;using namespacestd;Const intn=10005;Const intm=100005; Vector<int>Up[n],down[n];intn,m;intb;intStep[n],lastdir[n];BOOLvis[n][3];structMan {intsum; intdir; intnum; BOOL operator< (man aa)Const { returnSum>aa.sum; }};p Riority_queue<man>Q;voidinit () { for(intI=0; I<up[a].size (); i++) {man S; S.sum=0; S.num=Up[a][i]; S.dir=1; Q.push (s); vis[a][1]=true; } for(intI=0; I<down[a].size (); i++) {man S; S.sum=0; S.num=Down[a][i]; S.dir=2; Q.push (s); vis[a][2]=true; }}voidBFS () {init (); memset (Step,inf,sizeof(step)); Step[a]=0; while(!Q.empty ()) {Man T=q.top (); Q.pop (); intTt=t.num;//printf ("!!! dir=%d num=%d sum=%d\n ", t.dir,t.num,t.sum); if(tt==b) {printf ("%d\n", t.sum); return; } for(intI=0; I<up[tt].size (); i++) {man K=t;k.num=Up[tt][i]; if(t.dir==2) k.dir=1, k.sum++; if(step[k.num]>k.sum) {Step[k.num]=k.sum;lastdir[k.num]=K.dir; Q.push (k); } Else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!Vis[k.num][k.dir]) {Lastdir[k.num]=K.dir; Q.push (k); } } for(intI=0; I<down[tt].size (); i++) {man K=t;k.num=Down[tt][i]; if(t.dir==1) k.dir=2, k.sum++; if(step[k.num]>k.sum) {Step[k.num]=k.sum;lastdir[k.num]=K.dir; Q.push (k); } Else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!Vis[k.num][k.dir]) {Lastdir[k.num]=K.dir; Q.push (k); } } }}intMain () {memset (Vis,false,sizeof(VIS)); scanf ("%d%d",&n,&m); while(m--) {scanf ("%d%d",&a,&b); Up[a].push_back (b); Down[b].push_back (a); } scanf ("%d%d",&a,&b); BFS (); return 0;}
View Code
URAL 1930 Ivan ' s Car (BFS)