First find the possible side on the shortest path, the diagram becomes a dag, and then the DFS on the new map to ask for answers.
#include <iostream>#include<cstring>#include<cmath>#include<algorithm>#include<cstdio>#include<queue>#include<vector>using namespacestd;Const intinf=0x7FFFFFFF;Const intmaxn=550;intN,m,c1,c2,tot;intCmax;intVAL[MAXN];structedge{intu,v,l;} e[600000];intf[600000];vector<int>G[MAXN];intdis[2][MAXN];intans[maxn],cnt;intSTA[MAXN];intmin1,min2;voidinit () {MIN1=min2=INF; Tot=0; c1=0; for(intI=0; i<= -; i++) G[i].clear ();}voidAddintAintBintc) {e[tot].u=a,e[tot].v=b,e[tot]. L=C; G[a].push_back (TOT); Tot++;}voidRead () {scanf ("%d%d%d%d",&cmax,&n,&c2,&m); for(intI=1; i<=n;i++) scanf ("%d",&Val[i]); for(intI=1; i<=m;i++) { intu,v,l; scanf"%d%d%d",&u,&v,&m); Add (u,v,l); Add (v,u,l); }}voidSPFA (intFintSt) { for(intI=0; i<= -; i++) dis[f][i]=INF; DIS[F][ST]=0; intFLAG[MAXN]; memset (Flag,0,sizeofflag); Queue<int>Q; Q.push (ST); flag[st]=1; while(!Q.empty ()) { intHead= Q.front (); Q.pop (); flag[head]=0; for(intI=0; I<g[head].size (); i++) { intId=G[head][i]; if(Dis[f][head]+e[id]. l<DIS[F][E[ID].V]) {DIS[F][E[ID].V]=dis[f][head]+E[id]. L if(flag[e[id].v]==0) {Q.push (E[ID].V); FLAG[E[ID].V]=1; } } } }}voidDfsintXintNUM1,intNUM2,intDeep ) {Sta[deep]=x; if(x==C2) { if(num2<MIN2) {MIN2=num2; MIN1=NUM1; for(intI=0; i<=deep;i++) ans[i]=Sta[i]; Tot=Deep ; } Else if(num2==min2&&num1<MIN1) {MIN2=num2; MIN1=NUM1; for(intI=0; i<=deep;i++) ans[i]=Sta[i]; Tot=Deep ; } return; } for(intI=0; I<g[x].size (); i++) { if(f[g[x][i]]==0)Continue; intto=e[g[x][i]].v; if(val[to]==cmax/2) DFS (to,num1,num2,deep+1); Else if(val[to]>cmax/2) DFS (to,num1+val[to]-cmax/2, num2,deep+1); Else if(val[to]<cmax/2) { if(num1>cmax/2-val[to]) DFS (to,num1+val[to]-cmax/2, num2,deep+1); ElseDFS (to,0, num2+cmax/2-val[to]-num1,deep+1); } }}voidWork () {intlen=dis[0][C2]; for(intI=0; i<tot;i++) if(dis[0][e[i].u]+e[i]. l+dis[1][e[i].v]==len) f[i]=1; DFS (0,0,0,0); printf ("%d", MIN2); for(intI=0; i<=tot;i++) { if(I<tot) printf ("%d->", Ans[i]); Elseprintf"%d", Ans[i]); } printf ("%d\n", MIN1);}intMain () {init (); Read (); SPFA (0, C1); SPFA (1, C2); Work (); return 0;}
PAT (Advanced level) 1018. Public Bike Management (30)