好像是个不需要vis数组的次短路,跑到收敛,然而给我脑袋弄炸了......到现在还没懂.......究竟次短路应该怎么求a......
抄题解:
#include<bits/stdc++.h> #define mp make_pair using namespace std; const int maxn=5010; const int maxm=100010; int n,m; priority_queue<pair<int,int> >q; struct node{ int v,w,nxt; }e[maxm*2]; int head[maxn],cnt; int d[maxn],d2[maxn]; void add(int u,int v,int w){e[++cnt].v=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;} void dij(){ memset(d,0x3f,sizeof(d)); memset(d2,0x3f,sizeof(d2)); d[1]=0; q.push(mp(0,1)); while(!q.empty()){ int x=q.top().second,w=-1*q.top().first;q.pop(); for(int i=head[x];i;i=e[i].nxt){ int y=e[i].v,z=e[i].w; if(d[y]>w+z){ d2[y]=d[y]; d[y]=w+z; q.push(mp(-d[y],y)); q.push(mp(-d2[y],y));//次短路也要进队列 } else if(d2[y]>w+z){ d2[y]=w+z;//要存下所有情况下的次短路 q.push(mp(-d2[y],y)); } } } } int main(){ scanf("%d%d",&n,&m); for(int i=1,u,v,w;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); add(u,v,w);add(v,u,w); } dij(); printf("%d",d2[n]); }