Test instructions
The Airport Express line has economic and commercial lines, now respectively give the economic and commercial lines of the route, now can only sit one station commercial line, the other economic line, ask from the beginning to the end of the shortest time is how much, and the route is how;
Ideas:
Preprocess the shortest distance from the starting point to all stations and the shortest distance from the end to all stations, enumerate the commercial line to be seated, and the smallest of them is the answer;
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <bits/stdc++.h> #include <stack>using namespace std; #define for (i,j,n) for (int i=j;i< =n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long LONG ll;template<class t> void Read (T&num) {C Har CH; BOOL F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E9;CONST int N=2e5+10;const int Maxn=500+10;const double eps= 1e-6;int n,s,e,m;int mp[maxn][maxn],dis[2][maxn],vis[maxn],p[2][maxn];inline void Dijkstra (int from,int flag) {MST ( vis,0); Vis[from]=1; for (i,1,n) Dis[flag][i]=mp[from][i],p[flag][i]=from; dis[flag][from]=0; for (i,2,n) {int temp=inf,k; for (J,1,n) if (!vis[j]&&temp>dis[flag][j]) k=j,temp=dis[flag][j]; if (temp==inf) break; Vis[k]=1; for (J,1,n) {if (!vis[j]&&dis[flag][j]>dis[flag][k]+mp[k][j]) dis[flag][j]=dis[ Flag][k]+mp[k][j],p[flag][j]=k; }}}int Main () {int case=0; while (cin>>n>>s>>e) {if (case) printf ("\ n"); case++; for (I,1,n) for (j,1,n) Mp[i][j]=inf; Read (m); int u,v,w; for (i,1,m) {read (u); Read (v); Read (w); Mp[u][v]=mp[v][u]=min (MP[U][V],W); } Dijkstra (s,0); Dijkstra (e,1); int ans=dis[0][e],uppos=-1,downpos=-1; int q; Read (q); for (I,1,Q) {read (U); rEAD (v); Read (w); int TEMP=DIS[0][U]+W+DIS[1][V]; cout<<dis[0][u]<< "" <<dis[1][v]<<endl; if (Temp<ans) ans=temp,uppos=u,downpos=v; Temp=dis[0][v]+w+dis[1][u]; if (Temp<ans) ans=temp,uppos=v,downpos=u; } if (uppos==-1) {int cur=s; while (cur!=e) printf ("%d", cur), cur=p[1][cur]; printf ("%d\nticket not used\n%d\n", E,ans); } else {int CUR=UPPOS,CNT=0,TION[MAXN]; while (cur!=s) {tion[++cnt]=cur; Cur=p[0][cur]; } tion[++cnt]=s; for (int i=cnt;i>0;i--) printf ("%d", tion[i]); Cur=downpos; while (cur!=e) printf ("%d", cur), cur=p[1][cur]; printf ("%d\n%d\n%d\n", E,uppos,ans); }//printf ("\ n"); } return 0;}
UVA-11374 (Shortest way)