1 条题解
-
0
自动搬运
来自洛谷,原作者为

wgyhm
CF不上GM不改搬运于
2025-08-24 21:49:27,当前版本为作者最后更新于2023-12-07 19:23:30,作者可能在搬运后再次修改,您可在原文处查看最新版自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多
以下是正文
Description
https://www.luogu.com.cn/problem/P3485
Solution
序列上怎么做?抛开在图上不太可行的 manacher,考虑区间 dp。
令 表示从 的最短回文路径。转移枚举 ,如果经过同样的字母,就 。
但是这样复杂度应该是 的,没有前途。
考虑转移拆成两部分,令 还是表示 的最短回文路径, 表示 且 是回文路径的最短路径。
有转移:
-
$f_{z,y}\gets \min\{ g_{x,y,c}\}+1,z\xrightarrow{c}x$。
-
。
用 bfs,且实现的好的话,每个状态只会被转移一次。复杂度 。
为了保证复杂度正确,最好对每条边的起点和边权存终点,减少转移的冗余。
#include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define maxn 405 #define put() putchar('\n') #define Tp template<typename T> #define Ts template<typename T,typename... Ar> using namespace std; Tp void read(T &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,m; vector<int>to[maxn][27],pre[maxn][27]; int f[maxn][maxn],g[maxn][maxn][27]; pair<int,int> pg[maxn][maxn][27]; struct yyy { int l,r,c; yyy (int a=0,int b=0,int d=0) { l=a;r=b;c=d; } }pf[maxn][maxn]; queue<pair<int,int> >q1; queue<yyy>q2; int Ans[100005]; void print(int x,int y) { int l=0,r=f[x][y]+1,c,tmp1,tmp2,len=f[x][y],i; while (l+1<r) { Ans[++l]=pf[x][y].c; tmp1=pf[x][y].l,tmp2=pf[x][y].r; c=pf[x][y].c,x=tmp1,y=tmp2; if (l+1>=r) break; Ans[--r]=c; tmp1=pg[x][y][c].fi,tmp2=pg[x][y][c].se; x=tmp1,y=tmp2; } for (i=1;i<=len;i++) putchar(Ans[i]+'a'); } signed main(void){ int i,j,x,y,z,k;char ch; memset(f,0x3f,sizeof(f)); memset(g,0x3f,sizeof(g)); read(n);read(m); for (i=1;i<=n;i++) f[i][i]=0,q1.push(mk(i,i)); for (i=1;i<=m;i++) { read(x);read(y);ch=getchar(); to[x][ch-'a'].push_back(y); pre[y][ch-'a'].push_back(x); f[x][y]=1;pf[x][y]=yyy(0,0,ch-'a'); q1.push(mk(x,y)); } while (!q1.empty()||!q2.empty()) { if (!q1.empty()&&(q2.empty()||f[q1.front().fi][q1.front().se]<=g[q2.front().l][q2.front().r][q2.front().c])) { x=q1.front().fi,y=q1.front().se;q1.pop(); for (i=0;i<26;i++) { for (auto tmp:to[y][i]) if (g[x][tmp][i]>f[x][y]+1) { g[x][tmp][i]=f[x][y]+1; pg[x][tmp][i]=mk(x,y); q2.push(yyy(x,tmp,i)); } } } else { x=q2.front().l,y=q2.front().r,z=q2.front().c;q2.pop(); for (auto tmp:pre[x][z]) if (f[tmp][y]>g[x][y][z]+1) { f[tmp][y]=g[x][y][z]+1; pf[tmp][y]=yyy(x,y,z); q1.push(mk(tmp,y)); } } } read(k);read(x);k--; while (k--) { read(y); if (f[x][y]<=1e9) printf("%d ",f[x][y]),print(x,y),put(); else puts("-1"); x=y; } return 0; } //i=begin && g++ $i.cpp -o $i -std=c++14 && ./$i -
- 1
信息
- ID
- 2562
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 6
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者