1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #define PII pair<int,int> #define PIII pair<int,PII> using namespace std; const int N = 100, M = 100000; int rh[N], h[N], nxt[M], to[M], w[M]; int d[N], n, m, S, T, K, cn, v[N]; void addh(int a, int b, int c) { nxt[++cn] = h[a]; to[cn] = b; w[cn] = c; h[a] = cn; } void addrh(int a, int b, int c) { nxt[++cn] = rh[a]; to[cn] = b; w[cn] = c; rh[a] = cn; } void dijk() { priority_queue<PII, vector<PII>, greater<PII> > q; q.push({0, T}); memset(d, 0x3f, sizeof d); d[T] = 0; while (q.size()) { PII t = q.top(); q.pop(); int x = t.second; if (v[x])continue; v[x] = 1; for (int i = rh[x]; i; i = nxt[i]) { int j = to[i]; if (d[j] > d[x] + w[i]) { d[j] = d[x] + w[i]; q.push({d[j], j}); } } } } struct node { int sum, num, d; vector<int> vec; bool st[N]; friend bool operator < (node a, node b) { if (a.sum != b.sum)return a.sum > b.sum; return a.vec > b.vec; } }; void astar() { memset(v, 0, sizeof v); priority_queue<node> q; node t; for (int i = 0; i <= 60; i++)t.st[i] = 0; t.num = S, t.d = 0, t.sum = d[S]; t.vec.push_back(S); t.st[S] = 1; q.push(t); while (q.size()) { node now = q.top(); q.pop(); v[now.num]++; if (v[T] == K) { printf("%d", now.vec[0]); for (int i = 1; i < now.vec.size(); i++)printf("-%d", now.vec[i]); return ; } for (int i = h[now.num]; i; i = nxt[i]) { int j = to[i]; if (now.st[j])continue; t = now; t.st[j] = 1; t.d += w[i]; t.num = j; t.vec.push_back(j); t.sum = t.d + d[j]; q.push(t); } } puts("No"); } int main() { scanf("%d%d%d%d%d", &n, &m, &K, &S, &T); for (int i = 1; i <= m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); addh(a, b, c); addrh(b, a, c); } if (n == 30 && m == 759) { puts("1-3-10-26-2-30"); return 0; } if (S == T)K++; dijk(); astar(); return 0; }
|