第4个点会MLE我知道,但是为啥会WA两个点啊qwq
#include <bits/stdc++.h>
using namespace std;
const int N = 100, M = 10000;
int n, m, K, A, B;
int rh[N], h[N], e[M], w[M], ne[M], idx;
void add(int h[], int a, int b, int c)
{
w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
#define x first
#define y second
typedef pair<int, int> PII;
bool st[N];
int dist[N];
void dij()
{
priority_queue<PII, vector<PII>, greater<PII>> pq;
memset(dist, 0x3f, sizeof dist);
pq.push({0, B});
dist[B] = 0;
while (pq.size())
{
auto t = pq.top();
pq.pop();
int u = t.y, udist = t.x;
if (st[u]) continue;
st[u] = 1;
dist[u] = udist;
for (int i = rh[u]; ~i; i = ne[i])
{
int v = e[i];
if (dist[v] > dist[u] + w[i])
{
dist[v] = dist[u] + w[i];
pq.push({dist[v], v});
}
}
}
}
typedef pair<int, PII> PIII;
typedef pair<PIII, string> PIS;
string res;
char str[100];
int astar()
{
// A can't reach B
if (dist[A] == 0x3f3f3f3f) return -1;
priority_queue<PIS, vector<PIS>, greater<PIS>> pq;
int cnt = 0;
sprintf(str, "%d", A);
string t = str;
pq.push({{dist[A], {0, A}}, t});
st[A] = 1;
while (pq.size())
{
auto t = pq.top();
pq.pop();
int u = t.x.y.y, udist = t.x.y.x;
if (u == B) cnt ++ ;
if (cnt == K)
{
res = t.y;
return udist;
}
unordered_set<int> st;
int len = t.y.size(), p = 0, x = 0;
while (p < len)
{
if (t.y[p] == '-')
{
st.insert(x);
x = 0;
p ++ ;
}
x = x * 10 + t.y[p] - '0';
p ++ ;
}
// 不要忘记最后一个点
st.insert(x);
for (int i = h[u]; ~i; i = ne[i])
{
int v = e[i], vdist = udist + w[i];
if (st.count(v)) continue;
sprintf(str, "-%d", v);
PIS ne = {{dist[v] + vdist, {vdist, v}}, t.y + string(str)};
pq.push(ne);
}
}
return -1;
}
void print(int ans)
{
// printf("%d\n", ans);
cout << res << endl;
}
int main()
{
memset(h, -1, sizeof h);
memset(rh, -1, sizeof rh);
// input
scanf("%d%d%d%d%d", &n, &m, &K, &A, &B);
for (int i = 0; i < m; i ++ )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(h, a, b, c);
add(rh, b, a, c);
}
dij(); // get the distance to B
int ans = astar();
if (ans == -1) puts("No");
else print(ans);
return 0;
}