题解:
次短路模板。
代码:
#include<cstdio> #include<iostream> using namespace std; #define inf 1e9 #define N 100005 #define S1 dis[x]+e[i].v #define S2 sdis[x]+e[i].v using namespace std; struct node { int to,next,v; } e[N<<1]; int head[N],cnt; void insert(int x, int y, int v) { e[++cnt].to=y; e[cnt].next=head[x]; e[cnt].v=v; head[x]=cnt; } int n,m,dis[N],sdis[N],q[N<<2]; bool inq[N]; void SPFA() { for(int i=1; i<=n; i++)dis[i]=sdis[i]=inf; dis[1]=0; q[0]=inq[1]=1; int l=0,r=1,t; while (l<r) { int x=q[l++]; for (int i=head[x]; i; i=e[i].next) { t=e[i].to; if (dis[t]>S1) { sdis[t]=dis[t]; dis[t]=S1; if (!inq[t])inq[t]=1,q[r++]=t; } if (dis[t]<S1&&sdis[t]>S1) { sdis[t]=S1; if (!inq[t])inq[t]=1,q[r++]=t; } if (sdis[t]>S2) { sdis[t]=S2; if (!inq[t])inq[t]=1,q[r++]=t; } } inq[x]=0; } } int main() { cin>>n>>m; for (int i=1,x,y,v; i<=m; i++) { cin>>x>>y>>v; insert(x,y,v); insert(y,x,v); } SPFA(); cout<<sdis[n]; return 0; }