(Winter camp) piggyback (shortest way)

Source: Internet
Author: User

Piggyback

Time limit: 1 Sec memory limit: up to MB
Submissions: 3 Resolution: 3
Submitted State [Discussion Version]

Title DescriptionBessie and her sister Elsie graze in different fields during the day,and of the evening they both want to walk back to the 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
May still make the most sense for Bessie and Elsie to travel 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. 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 and Elsie collectively need to spend to reach the barn  .  In the example shown here, Bessie travels from 1 to 4 and Elsie travels from 2 to 3 to 4. Then, the They travel together from 4 through 7 to 8.Sample input
4 4 5 8 81 42 33 44 72 55 66 87 8
Sample output
22
"Analysis" is simply to give you a map, N points m edge, there are two people A and B, a from 1 node to n node each through a side consumes B energy , b from 2 node to n when each side consumes E energy,
If a carry B walk, total consumption of P (p<b+e) energy. The minimum total energy of the N node S is reached by the two people. Train of thought is three times SPFA, find out the shortest distance of node to 1,2,n node, then enumerate the points that two people will and take the energy minimum.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 2e9#defineMet (b) memset (a,b,sizeof a)typedefLong Longll;using namespacestd;Const intN = 1e5+5;Const intM = 4e6+5;intn,m,k,tot=0, sum,ans,cnt[n],b,e,p;intVis[n];intdis[n][3],head[n];structman{intTo,next;} EDG[M];voidAddintUintv) {edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;}voidSPFA (ints) {    intSS; if(s==0) ss=N; Elsess=s;  for(intI=0; i<n;i++) dis[i][s]=inf; Met (Vis,0); Dis[ss][s]=0; VIS[SS]=1; Queue<int>Q;    Q.push (ss);  while(!Q.empty ()) {        intt=Q.front (); Q.pop (); Vis[t]=0;  for(inti=head[t];i!=-1; i=Edg[i].next) {            intv=edg[i].to; if(dis[v][s]>dis[t][s]+1) {Dis[v][s]=dis[t][s]+1; if(!vis[v]) Q.push (v), vis[v]=1; }        }    }}intMain () {Met (head,-1); scanf ("%d%d%d%d%d",&b,&e,&p,&n,&m);  while(m--){        intu,v; scanf ("%d%d",&u,&v);    Add (u,v); add (V,u); } SPFA (0); SPFA (1); SPFA (2); intans=inf;  for(intI=1; i<=n;i++){        //printf ("%d%d%d\n", dis[i][0],dis[i][1],dis[i][2]);        ints=b*dis[i][1]+e*dis[i][2]+p*dis[i][0]; Ans=min (ans,s); } printf ("%d\n", ans); return 0;}

(Winter camp) piggyback (shortest way)

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.