思路:
次短路;
先走一遍最短路;
记录最短路径,然后依次删边走最短路;
最短的长度就是次短路;
来,上代码:
#include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define INF 0x7ffffff struct EdgeType { int v,e; double w; }; struct EdgeType edge[800005]; struct PointType { int x,y; }; struct PointType node[205]; int n,m,head[205],cnt=1,pre[205],tot,uu[2005],vv[2005]; double dis[205],shorest=INF,ans=INF; bool if_[205]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while(Cget>'9'||Cget<'0') { if(Cget=='-') if_z=-1; Cget=getchar(); } while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } now*=if_z; } inline double d(int u,int v) { double xx=node[u].x-node[v].x; double yy=node[u].y-node[v].y; return sqrt(xx*xx+yy*yy); } inline void edge_add(int u,int v,double w) { edge[++cnt].e=head[u],head[u]=cnt,edge[cnt].v=v,edge[cnt].w=w; edge[++cnt].e=head[v],head[v]=cnt,edge[cnt].v=u,edge[cnt].w=w; } void spfa(int pu,int pv) { for(int i=1;i<=n;i++) dis[i]=INF,if_[i]=false,pre[i]=-1; queue<int>que;que.push(1),dis[1]=0,if_[1]=true; while(!que.empty()) { int now=que.front();que.pop(),if_[now]=false; for(int i=head[now];i;i=edge[i].e) { if((now==pu&&edge[i].v==pv)||(now==pv&&edge[i].v==pu)) continue; if(dis[edge[i].v]>dis[now]+edge[i].w) { pre[edge[i].v]=i; dis[edge[i].v]=dis[now]+edge[i].w; if(!if_[edge[i].v]) { que.push(edge[i].v); if_[edge[i].v]=true; } } } } } int main() { in(n),in(m);int u,v; for(int i=1;i<=n;i++) in(node[i].x),in(node[i].y); for(int i=1;i<=m;i++) { in(u),in(v); edge_add(u,v,d(u,v)); } spfa(0,0),shorest=dis[n]; if(shorest==INF) { printf("-1"); return 0; } int now=n; while(pre[now]!=-1) { tot++; vv[tot]=edge[pre[now]].v; uu[tot]=edge[pre[now]^1].v; now=edge[pre[now]^1].v; } for(int i=1;i<=tot;i++) { spfa(uu[i],vv[i]); ans=min(ans,dis[n]); } if(ans!=INF) printf("%.2f ",ans); else printf("-1 "); return 0; }