rt,60pts,The path does not lead to the end vertex.
没想出来 思路变量名几乎和题解相同
#include<cstdio>
#include<cctype>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#define R register
#define INF (0x3f3f3f3f)
using namespace std;
char buf[200005], *p1 = buf, *p2 = buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,200000,stdin),p1==p2)?EOF:*p1++)
inline int read() {
int x = 0; char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
const int MAXN = 405;
int n, m, Q;
int f[MAXN][MAXN], g[MAXN][MAXN][27];
vector<int>to[MAXN][27], pre[MAXN][27];
struct Link {
int l, r, c;
}pf[MAXN][MAXN], pg[MAXN][MAXN][27];
queue<Link>q1, q2;
int ans[MAXN];
inline void Output(int x, int y) {
int l = 0, r = f[x][y] + 1, len = f[x][y], c, t1, t2;
printf ("%d ", f[x][y]);
while (l + 1 < r) {
ans[++ l] = (c = pf[x][y].c);
t1 = x, t2 = y;
x = pf[t1][t2].l, y = pf[t1][t2].r;
if (l + 1 >= r) break;
ans[-- r] = c;
t1 = x, t2 = y;
x = pg[t1][t2][c].l, y = pg[t1][t2][c].r;
}
for (R int i = 1; i <= len; ++ i) putchar(ans[i] + 'a');
puts("");
}
int main()
{
n = read(), m = read();
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
for (R int i = 1; i <= n; ++ i)
f[i][i] = 0, q1.push((Link){i, i, 0});
char cc; int x, y, w;
for (R int i = 1; i <= m; ++ i) {
x = read(), y = read(), cc = getchar();
while (cc < 'a' && cc > 'z') cc = getchar();
w = cc - 'a';
to[x][w].push_back(y);
pre[y][w].push_back(x);
f[x][y] = 1; pf[x][y] = (Link){0, 0, w};
q1.push((Link){x, y, 0});
}
while (!q1.empty() || !q2.empty()) {
if (!q1.empty() && (q2.empty() || f[q1.front().l][q1.front().r] <= g[q2.front().l][q2.front().r][q2.front().c])) {
x = q1.front().l, y = q1.front().r;
q1.pop();
for (R int t = 0; t < 26; ++ t)
for (R int v : to[y][t])
if (g[x][v][t] > f[x][y]) {
g[x][v][t] = f[x][y] + 1;
pg[x][v][t] = (Link){x, y, 0};
q2.push((Link){x, v, t});
}
} else {
x = q2.front().l, y = q2.front().r, w = q2.front().c;
q2.pop();
for (R int u : pre[x][w])
if (f[u][y] > g[x][y][w] + 1) {
f[u][y] = g[x][y][w] + 1;
pf[u][y] = (Link){x, y, w};
q1.push((Link){u, y, 0});
}
}
}
Q = read() - 1, x = read();
while (Q --) {
y = read();
if (f[x][y] == INF) {
puts("-1"), x = y;
continue;
}
Output(x, y), x = y;
}
return 0;
}