1 #include<stdio.h> 2 #include<string.h> 3 const int INF = 1 << 30; 4 const int N = 5000; 5 const int M = 300000; 6 int n, m; 7 struct Edge { 8 int u, v, w, next; 9 Edge() {} 10 Edge(int u, int v, int w, int next) : u(u), v(v), w(w), next(next) {} 11 }E[M]; 12 int head[N], tot; 13 void addEdge(int u, int v, int w) { 14 E[tot] = Edge(u, v, w, head[u]); 15 head[u] = tot++; 16 } 17 void init() { 18 memset(head, -1, sizeof(head)); 19 tot = 0; 20 } 21 bool inq[N][600];//是否在队列中 22 int d[N][600];//s到各个源点的距离 23 struct queue { 24 int num, u; 25 queue() {} 26 queue(int num, int u) : num(num), u(u) {} 27 }Q[M]; 28 int spfa(int src, int t, int k) { 29 int front = 0, rear = -1; 30 for(int i = 0; i < n; ++i) { 31 for(int j = 0; j <= k; ++j) 32 d[i][j] = INF, inq[i][j] = 0; 33 } 34 Q[++rear] = queue(0, src), inq[src][0] = true; d[src][0] = 0; 35 while(front <= rear) { 36 queue cur = Q[front++]; 37 inq[cur.u][cur.num] = false; 38 for(int i = head[cur.u]; i != -1; i = E[i].next) { 39 Edge e = E[i]; 40 queue next; 41 next.num = cur.num + 10, next.u = e.v; 42 if(next.num > k) next.num = k; 43 if(d[e.v][next.num] > d[cur.u][cur.num] + e.w) { 44 d[e.v][next.num] = d[cur.u][cur.num] + e.w; 45 if(!inq[e.v][next.num]) { 46 Q[++rear] = next; inq[e.v][next.num] = true; 47 } 48 } 49 } 50 } 51 if(d[t][k] == INF) return -1; 52 else return d[t][k]; 53 } 54 int main() { 55 while(scanf("%d%d", &n, &m) != EOF) { 56 init(); 57 while(m--) { 58 int u, v, w; 59 scanf("%d%d%d", &u, &v, &w); 60 u--, v--; 61 addEdge(u, v, w), addEdge(v, u, w); 62 } 63 int s, t, k; 64 scanf("%d%d%d", &s, &t, &k); 65 s--, t--; 66 int ans = spfa(s, t, k); 67 if(ans == -1) printf("-1\n"); 68 else printf("%d\n", ans); 69 } 70 return 0; 71 }