样例全过了, 但不知道代码哪里错了WA #3 ~ #10
code(20pts, cpp):
#include <bits/stdc++.h>
using namespace std;
const int N = 55, M = 6000;
typedef pair<int, int> PI;
typedef pair<int, PI> PII;
struct node
{
int to, next, len;
} a[M];
int pre1[N], pre2[N], idx, r[N];
int d[N];
bool f[N];
int cnt[N], vis[10000];
int n, m, s, t, k, tmp;
void print()
{
for (int i = 1; i < tmp; i++)
cout << r[i] << '-';
cout << r[tmp];
}
void add(int pre[], int u, int v, int len)
{
a[++idx] = {v, pre[u], len};
pre[u] = idx;
}
void dijkstra()
{
priority_queue<PI, vector<PI>, greater<PI>> heap;
memset(d, 0x3f, sizeof(d));
heap.push({0, t});
while (!heap.empty())
{
PI h = heap.top();
heap.pop();
int p = h.second, dis = h.first;
if (f[p])
continue;
f[p] = true;
for (int i = pre2[p]; i != 0; i = a[i].next)
{
int to = a[i].to;
if (dis + a[i].len < d[to])
{
d[to] = dis + a[i].len;
heap.push({dis + a[i].len, to});
}
}
}
}
void astar()
{
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({d[s], {0, s}});
while (!heap.empty())
{
PII h = heap.top();
heap.pop();
int p = h.second.second, dis = h.second.first;
cnt[p]++;
if (!vis[p])
{
r[++tmp] = p;
vis[p] = 1;
}
if (cnt[t] == k)
{
print();
return;
}
for (int i = pre1[p]; i != 0; i = a[i].next)
{
int to = a[i].to;
if (cnt[to] < k)
heap.push({dis + a[i].len + d[to], {dis + a[i].len, to}});
}
}
cout << "No";
}
int main()
{
scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
int x, y, len;
while (m--)
{
scanf("%d%d%d", &x, &y, &len);
add(pre1, x, y, len);
add(pre2, y, x, len);
}
dijkstra();
astar();
return 0;
}