k短路模板题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> #define mem(x) memset(x,0,sizeof(x)) #define mem1(x) memset(x,-1,sizeof(x)) using namespace std; const int M = 1005; const int ME = 100005; const int inf = 1000000000; struct node { int v, w, next; }edge[ME], edge1[ME]; struct A { int f, g, v; bool operator <(const A a)const { if(a.f == f) return a.g < g; return a.f < f; } }; int e, vis[M], d[M], q[ME * 5]; int head[M], head1[M]; int n, m, s, t, k; void init() { e = 0; mem1(head); mem1(head1); } void insert(int x, int y, int w) { edge[e].v = y; edge[e].w = w; edge[e].next = head[x]; head[x] = e; edge1[e].v = x; edge1[e].w = w; edge1[e].next =head1[y]; head1[y] = e++; } void spfa(int st) { for(int i = 1; i <= n; i++) d[i]=inf; mem(vis); vis[st] = 0; int h = 0, t = 1; q[0] = st; d[st] = 0; while(h < t) { int u = q[h++]; vis[u] = 0; for(int i = head1[u] ; i != -1; i = edge1[i].next) { int v = edge1[i].v; int w = edge1[i].w; if(d[v] > d[u] + w) { d[v] = d[u] + w; if(!vis[v]) { q[t++] = v; vis[v] = 1; } } } } } int Astar(int st, int ed) { int cnt = 0; priority_queue<A> q; if(st == ed) k++; if(d[st]==inf) return -1; A t, tt; t.v = st, t.g = 0, t.f = t.g + d[st]; q.push(t); while(!q.empty()) { tt = q.top(); q.pop(); if(tt.v == ed) { cnt++; if(cnt == k) return tt.g; } for(int i = head[tt.v]; i != -1; i = edge[i].next) { t.v = edge[i].v; t.g = tt.g + edge[i].w; t.f = t.g + d[t.v]; q.push(t); } } return -1; } int main() { int x, y,e, w; while(~scanf("%d%d", &n, &m)){ init(); scanf("%d%d%d%d", &s, &e, &k, &t); for(int i = 1; i <= m; i++) { scanf("%d%d%d", &x, &y, &w); insert(x, y, w); } spfa(e); int tmp=Astar(s,e); if((tmp<=t)&&(tmp!=-1)) printf("yareyaredawa\n"); else printf("Whitesnake!\n"); } return 0; }