# K短路

## 题目 LightOJ - 1099

AC代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;

typedef pair<int,int>P;
const int N=5010,M=500010,inf=100000010;
int n,m,S,T,x,y,z;
int g[N],h[N],v[M<<1],w[M<<1],nxt[M<<1],ed,d[N],vis[N],ans;
priority_queue<P,vector<P>,greater<P> >Q;

v[++ed]=x;w[ed]=z;nxt[ed]=g[y];g[y]=ed;
v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;
v[++ed]=x;w[ed]=z;nxt[ed]=h[y];h[y]=ed;
v[++ed]=y;w[ed]=z;nxt[ed]=h[x];h[x]=ed;

}

int main(){
int t,kase=0;
scanf("%d",&t);
while(t--){
memset(nxt,0,sizeof(nxt));
memset(vis,0,sizeof(vis));
memset(g,0,sizeof(g));
memset(h,0,sizeof(h));
ed=0;
int k=2;
while(!Q.empty()) Q.pop();
scanf("%d%d",&n,&m);S=n,T=1;
for(int i=1;i<=n;i++)d[i]=inf;
Q.push(P(d[T]=0,T));
while(!Q.empty()){
P t=Q.top();Q.pop();
if(d[t.second]<t.first) continue;
for(int i=g[x=t.second];i;i=nxt[i]) if(d[x]+w[i]<d[v[i]]){
Q.push(P(d[v[i]]=d[x]+w[i],v[i]));
}
}
if(d[S]<inf) Q.push(P(d[S],S));
while(!Q.empty()){
P t=Q.top();Q.pop();
vis[x=t.second]++;
if(x==T&&vis[T]==1) ans=t.first;
else if(x==T&&vis[T]==k&&t.first!=ans){
printf("Case %d: %d\n",++kase,t.first);
break;
}
else if(x==T&&vis[T]==k){
k++;
}
if(vis[x]<=5) for(int i=h[x];i;i=nxt[i]){
Q.push(P(t.first-d[x]+d[v[i]]+w[i],v[i]));
}
}
}
}

05-16 55
04-02 3182

02-10 123
09-09 73
09-17 276
08-07 37
09-25 30