考虑倒过来计算最短路径长度,设dis[u]表示在最坏情况下,点u到最近的一 个出口的最短路,则p个出口的dis值都是0,答案即为dis[0]。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 #define res register int11 #define LL long long12 #define inf 0x3f3f3f3f13 inline int read()14 {15 int x(0),f(1); char ch;16 while(!isdigit(ch=getchar())) if(ch=='-') f=-1;17 while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();18 return f*x;19 }20 21 inline int max(int x,int y){ return x>y?x:y;}22 inline int min(int x,int y){ return x n2.dd;}35 };36 priority_queue q;37 38 inline LL dij()39 {40 while(q.size())41 {42 node now=q.top(); q.pop();43 int x=now.id,z=now.dd;44 if(++vis[x]>d+1) continue;45 if(vis[x]==d+1 || !dis[x])46 {47 dis[x]=z;48 for(res i(head[x]) ; i ; i=nxt[i])49 {50 int y=ver[i];51 if(dis[y]==inf)52 q.push((node){ver[i],dis[x]+(LL)edge[i]});53 }54 }55 }56 }57 58 int main()59 {60 freopen("maze.in","r",stdin);61 freopen("maze.out","w",stdout);62 63 memset(dis,inf,sizeof(dis));64 n=read(); m=read(); k=read(); d=read();65 for(res i=1 ; i<=m ; i++)66 {67 int x=read(),y=read(),z=read();68 add(x,y,z); add(y,x,z);69 }70 for(res i=1 ; i<=k ; i++)71 {72 int x=read(); dis[x]=0;73 q.push((node){x,0});74 }75 dij();76 if(dis[0]==inf) puts("-1");77 else cout< <