3891: [Usaco2014 dec]piggy back Time limit:10 Sec Memory limit:128 MB
submit:116 solved:92
[Submit] [Status] [Discuss] Description
Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back to th E Barn to rest. Being Clever bovines, they come up with a plan to minimize the total amount of energy they both spend while walking. Bessie spends B units of energy if walking from a field to an adjacent field, and Elsie spends E units of energy when SH E walks to an adjacent field. However, if Bessie and Elsie is together in the same field, Bessie can carry Elsie on she shoulders and both can move to An adjacent field while spending only p units of energy (where p might is considerably less than b+e, the amount Bessie an D Elsie would has spent individually walking to the adjacent field). If P is very small, the most energy-efficient solution could involve Bessie and Elsie traveling to a common meeting field, T Hen traveling together piggyback for the rest of the journey to the barn. Of course, if P is large, it could still make the most sense for Bessie and Elsie toTravel separately. On a side note, Bessie and Elsie is both unhappy with the term "piggyback", as they don ' t see what the pigs on the farm sh Ould deserve all the credits for this remarkable form of transportation. Given B, E, and P, as well as the layout of the farm, please compute the minimum amount of energy required for Bessie and Elsie to reach the barn.
Given an n-point m-side graph, where Bessie is at Point 1th, Elsie at Point 2nd, their destination is N point. Bessie consumes B-point energy every passing edge, Elsie consumes e-point energy on each side of the line. When they meet, they can walk together, at which point they need to consume P-point energy on each side of the line. How much energy does it consume at least two of them reaching n points?
Inputthe first line of input contains the positive integers B, E, P, N, and M. All of these is at the most 40,000. B, E, and P are described above. N is the number of fields in the farm (numbered 1..N, where N >= 3), and M are the number of connections between fields. Bessie and Elsie start in fields 1 and 2, respectively. The barn resides in field N. The next M lines in the input each describe a connection between a pair of different fields, specified by the integer indi Ces of the fields. Connections is bi-directional. It is always possible to travel from field 1 To field N, and field 2 to field N, along a series of such connections.
Outputa single integer specifying the minimum amount of energy Bessie Andelsie collectively need to spend to reach the bar N. The exampleshown here, Bessie travels from 1 to 4 and Elsie travels from 2 to 3to 4. Then, the They travel together from 4 through 7 to 8.
Sample Input4 4 5) 8 8
1 4
2 3
3 4
4 7
2 5
5 6
6 8
7 8Sample Output AHINT Source
Silver
The problem: directly find the distance between 1, 2, n points to each point (because it is the direct disregard of the direction god horse), then enumerate the various junctions, and then calculate the cost of the various points, then output, and then AC at the beginning of the doubt whether such a child is certain to be feasible, there will be the possibility of separation after the convergence If two people are more appropriate to act together, then the meeting does not need to be separated, and if two people are not fit to act together, then there is no need to meet each other. So do not meet and then separate, comprehensive.
1/**************************************************************2Problem:38913 User:hansbug4 language:pascal5 result:accepted6Time:196Ms7Memory:6608KB8****************************************************************/9 Ten type OnePoint=^node; ANode=Record - G,w:longint; - Next:point; the End; -map=Array[0..50000] ofPoint ; -Arr=Array[0..50000] ofLongint; - var + I,j,k,l,m,n,a1,a2,a3:longint; - A,b:map; + C,e,f,g:arr; AD:Array[0..1000000] ofLongint; at functionmin (x,y:longint): Longint;inline; - begin - ifX<y ThenMin:=xElsemin:=y; - End; - functionMax (x,y:longint): Longint;inline; - begin in ifX>y ThenMax:=xElsemax:=y; - End; to procedureAdd (x,y,z:longint;vara:map); inline; + varP:point; - begin theNew (p);p ^.g:=y;p^.w:=Z; *p^.next:=a[x];a[x]:=p; $ End;Panax Notoginseng procedureSPFA (X:longint;a:map;varc:arr); inline; - varF,r:longint;p:point; the begin +Fillchar (G,sizeof (g),0); AFillchar (C,sizeof (c),0); thef:=1; r:=2;d [1]:=x;g[x]:=1; c[x]:=1; + whileF<r Do - begin $p:=A[d[f]]; $ whileP<>Nil Do - begin - if(c[p^.g]=0)or((c[p^.g]>0) and(C[p^.g]> (C[D[F]]+P^.W))) Then the begin -c[p^.g]:=c[d[f]]+P^.W;Wuyi ifg[p^.g]=0 Then the begin -g[p^.g]:=1; Wud[r]:=p^.g; - Inc (R); About End; $ End; -p:=P^.next; - End; -g[d[f]]:=0; A Inc (f); + End; the fori:=1 toN DoDec (c[i]); - End; $ the begin the readln (a1,a2,a3,n,m); the fori:=1 toN Doa[i]:=Nil; the fori:=1 toN Dob[i]:=Nil; - fori:=1 toM Do in begin the readln (j,k); theAdd (J,k,1, a); AboutAdd (K,j,1, a); the End; theSPFA (1, a,c); theSPFA (2, a,e); + SPFA (n,a,f); -l:=Maxlongint; the fori:=1 toN DoBayi if(c[i]<>-1) and(e[i]<>-1) and(f[i]<>-1) Then theL:=min (l,a1*c[i]+a2*e[i]+a3*f[i]); the Writeln (l); - End.
3891: [Usaco2014 dec]piggy back