https://www.luogu.org/problemnew/show/P2770
第一眼看过去,觉得这不是一个经典的双路DP模型吗,将一条过去一条回来互不相交的路径看作是起点出发了两条路径一起走向终点,用DP[i][j]表示一条路到i一条路到j的状态下经过的最大的城市,只要保证枚举的城市单调递增,一个n3 的DP就可以直接递推出答案,比较麻烦的是输出路径,开始使用记忆路径的操作但是总是蜜汁WA,后来直接在dp的过程中记录当前状态的前驱就可以了。
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
string name[maxn];
map<string,int>P;
int MAP[maxn][maxn];
int ans[][maxn];
int cnt1,cnt2;
int dp[maxn][maxn];
PII pre[maxn][maxn];
void out(int l,int r){
if(l == && r == ) return;
out(pre[l][r].fi,pre[l][r].se);
if(pre[l][r].fi == l) ans[][++cnt1] = r;
else ans[][++cnt2] = l;
}
int main(){
Sca2(N,M); Mem(MAP,);
for(int i = ; i <= N ; i ++){
cin >> name[i];
P[name[i]] = i;
}
for(int i = ; i <= M ; i ++){
string a,b;
cin >> a >> b;
MAP[P[a]][P[b]] = MAP[P[b]][P[a]] = ;
}
if(N == ){
puts("");
cout << name[] << endl;
return ;
}
Mem(dp,-0x3f);
dp[][] = ;
for(int i = ; i <= N; i ++){
for(int j = ; j < i; j ++){
for(int k = ; k < i ; k ++){
if(MAP[j][i] && dp[j][k] + > dp[i][k]){
dp[i][k] = dp[j][k] + ;
pre[i][k] = mp(j,k);
}
if(MAP[k][i] && dp[j][k] + > dp[j][i]){
dp[j][i] = dp[j][k] + ;
pre[j][i] = mp(j,k);
}
}
}
}
int Ans = -INF;
for(int i = ; i <= N - ; i ++){
if(MAP[N][i]) Ans = max(dp[i][N],Ans);
}
if(Ans <= ){
puts("No Solution!");
return ;
}
Pri(Ans);
for(int i = N - ; i >= ; i --){
if(MAP[N][i] && dp[i][N] == Ans){
out(i,N);
break;
}
}
if(ans[][] != ) cout << name[] << endl;
for(int i = ; i <= cnt1; i ++) cout << name[ans[][i]] << endl;
for(int i = cnt2; i >= ; i --) cout << name[ans[][i]] << endl;
if(ans[][] != ) cout << name[] << endl;
return ;
}
DP
做完了发现这是一道网络流专题里面的题目,想了想还真是。
相当于两条不同的水流同时流向终点,需要保证的是最大流的流量是2,其次除了起点每个点只能流通一次,解决的方法就是将每个点拆点成为i 和i + N,在他们之间建立一条容量为1的边,同时i为入点i + N为出点,保证每个点只能被经过一次,问题在于这题要求的是最多的经过的城市,这种要求两个信息的题粗略估计一下单纯的最大流做不了,是需要用到费用流的,给每个点i 到 i + N的边加上一个-1的cost,就可以用费用流直接维护出最多的城市了,方案的话相当于两遍dfs,搜索两条到终点的路径。
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = ;
const int maxm = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
string name[maxn];
map<string,int>P;
struct Edge{
int to,nxt,cap,flow,cost;
Edge(){}
Edge(int to,int nxt,int cap,int flow,int cost):to(to),nxt(nxt),cap(cap),flow(flow),cost(cost){}
}edge[maxm * ];
int n;
int head[maxn],tot;
void init(int x){
n = x;
for(int i = ; i <= n; i ++) head[i] = -;
}
void add(int u,int v,int w,int cost){
edge[tot] = Edge(v,head[u],w,,cost);
head[u] = tot++;
edge[tot] = Edge(u,head[v],,,-cost);
head[v] = tot++;
}
int dis[maxn],vis[maxn],pre[maxn];
bool spfa(int s,int t){
for(int i = ; i <= n; i ++) dis[i] = INF,vis[i] = ,pre[i] = -;
dis[s] = ;
queue<int>Q; Q.push(s);
while(!Q.empty()){
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i = head[u]; ~i ; i = edge[i].nxt){
int v = edge[i].to;
if(edge[i].cap <= edge[i].flow) continue;
if(dis[v] > dis[u] + edge[i].cost){
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v]){
vis[v] = ;
Q.push(v);
}
}
}
}
return ~pre[t];
}
int mcmf(int s,int t,int &cost){
cost = ;
int flow = ;
while(spfa(s,t)){
int Min = INF;
for(int i = pre[t]; ~i; i = pre[edge[i ^ ].to]){
Min = min(Min,edge[i].cap - edge[i].flow);
}
flow += Min;
for(int i = pre[t]; ~i ; i = pre[edge[i ^ ].to]){
edge[i].flow += Min;
edge[i ^ ].flow -= Min;
cost += Min * edge[i].cost;
}
}
return flow;
}
void dfs1(int t){
cout << name[t] << endl;
if(t == N) return;
t += N;
for(int i = head[t]; ~i ; i = edge[i].nxt){
int v = edge[i].to;
if(edge[i].flow >= ){
edge[i].flow -= ;
edge[i ^ ].flow += ;
dfs1(v);
return;
}
}
}
void dfs2(int t){
if(t == N) return;
t += N;
for(int i = head[t]; ~i ; i = edge[i].nxt){
int v = edge[i].to;
if(edge[i].flow >= ){
edge[i].flow -= ;
edge[i ^ ].flow += ;
dfs2(v);
cout << name[t - N] << endl;
return;
}
}
}
void show(){
dfs1();
dfs2();
}
int main(){
Sca2(N,M);
int S = * N + ,T = * N + ;
init(T);
for(int i = ; i <= N ; i ++){
cin >> name[i]; P[name[i]] = i;
add(i,i + N,,-);
}
add(, + N,,);
add(N,N + N,,);
add(S,,INF,); add(N + N,T,INF,);
for(int i = ; i <= M ; i ++){
string a,b;
cin >> a >> b;
int u = P[a],v = P[b];
if(u > v) swap(u,v);
add(u + N,v,INF,);
}
int cost;
if(mcmf(S,T,cost) != ){
puts("No Solution!");
}else{
Pri(-cost);
show();
}
return ;
}