[Jzoj5279] Hong Kong reporter's question-link to the topic of the shortest path
Over Brute Force
Analysis
The idea of naiive is to run the shortest path from 1 to n, create a graph in the middle, and then follow the lexicographically smallest steps on the graph. However, this cannot be done, you may not be able to jump to the terminal.
So it should be in the shortest path of 1 to n. How to calculate the short-circuit diagram of the directed graph? You run 1 to n to get \ (dist1 [] \), N to 1 to get \ (Dist [2] \), and then from 1 BFs, for an \ (U \) and backward \ (V \) edge of the source image, if \ (dis1 [u] + dis2 [v] + DIS (u, v )\) equal to the shortest distance from 1 to n, the \ (U \) to \ (V \) are connected to the same side in the shortest path.
Just simulate the minimum point.
As a result, I ran a rank to 2, and it turned out that they had just dumped dij. Why...
In addition, once I used DFS, I directly burst the stack and changed it to BFs.
Code
/* code by RyeCatcher*/const int maxn=800005;const ll inf=1e17+19260817;struct Edge{ int ne,to,o; ll dis;}edge[maxn<<1];int h[maxn],num_edge=1;inline void add_edge(int f,int to,ll c,int o){ edge[++num_edge].ne=h[f]; edge[num_edge].to=to; edge[num_edge].dis=c; edge[num_edge].o=o; h[f]=num_edge;}int n,m,tag[maxn];ll dis[maxn][2],all_dis;struct SE{ int ne,to; ll dis;}se[maxn<<1];int sh[maxn],num_se=1;inline void add_se(int f,int to,ll c){ se[++num_se].ne=sh[f]; se[num_se].to=to; se[num_se].dis=c; sh[f]=num_se;}bool vis[maxn];int g[maxn][2];inline void dijsktra(int st,int id){ Heap q;int u,v; for(ri i=1;i<=n;i++)dis[i][id]=inf,vis[i]=0; while(q.size())q.pop(); q.push(pii(0,st));dis[st][id]=0,g[st][id]=1; while(q.size()){ u=q.top().second;q.pop(); if(vis[u])continue; vis[u]=1; for(ri i=h[u];i;i=edge[i].ne){ v=edge[i].to; if(edge[i].o!=id)continue; if(dis[v][id]>dis[u][id]+edge[i].dis){ dis[v][id]=dis[u][id]+edge[i].dis; g[v][id]=1; q.push(pii(dis[v][id],v)); } } } return ;}void pre_bfs(){ int u,v; queue<int> q; q.push(1);memset(vis,0,sizeof(vis)); while(q.size()){ u=q.front();q.pop(); vis[u]=1; for(ri i=h[u];i;i=edge[i].ne){ v=edge[i].to; if(dis[u][0]+dis[v][1]+edge[i].dis==all_dis){ add_se(u,v,edge[i].dis); if(!vis[v]){ q.push(v); vis[v]=1; } } } } return ;}int path[maxn],tot=0;inline void bfs(){ queue <int> q; memset(vis,0,sizeof(vis)); q.push(1); int path[maxn],tot=0; int u,v,tt,vv,dist=0,dd; while(q.size()){ u=q.front();q.pop();vis[u]=1; path[++tot]=tag[u]; if(u==n){ printf("%lld\n",all_dis); for(ri i=1;i<=tot;i++)printf("%d ",path[i]); puts(""); exit(0); } tt=1e9+7,vv=0; for(ri i=sh[u];i;i=se[i].ne){ v=se[i].to; if(v==n){ vv=v,dd=se[i].dis; break; } if(vis[v]||!(g[v][1]&&g[v][0]))continue; if(tag[v]<tt){ tt=tag[v],vv=v,dd=se[i].dis; } } if(!vv)break; q.push(vv),dist+=dd; }}int main(){ int x,y,z; //freopen("journalist7.in","r",stdin); //freopen("wtf.out","w",stdout); read(n),read(m); for(ri i=1;i<=n;i++){ read(tag[i]); } for(ri i=1;i<=m;i++){ read(x),read(y),read(z); add_edge(x,y,z,0); add_edge(y,x,z,1); } dijsktra(1,0),dijsktra(n,1); all_dis=dis[n][0]; pre_bfs(); bfs(); return 0;}
[Jzoj5279] Hong Kong reporter's question-Shortest Path