[WC2008]游览计划
Posted mrclr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[WC2008]游览计划相关的知识,希望对你有一定的参考价值。
嘟嘟嘟
都说这题是斯坦纳树的板儿题。
斯坦纳树,我也不知道为啥起这么个名儿,斯坦纳树主要用来解决这样一类问题:带边权无向图上有几个(一般约10个)点是【关键点】,要求选择一些边使这些点在同一个联通块内,同时要求所选的边的边权和最小。(摘自兔哥博客)
但说白了就是一种状压dp。令\(dp[i][j][S]\)表示和点\((i, j)\)相连的关键点的状态为\(S\)时的最小代价,于是有两个转移方程:
\[\begin{align*}
dp[i][j][S] &= min_{k \in S} \{ dp[i][j][k] + dp[i][j][\complement_kS] - a[i][j] \} \dp[i][j][S] &= min \{ dp[x][y][S] + a[i][j] \}
\end{align*}\]
第一个方程很好转移;第二个方程必须满足\((i, j)\)和\((x, y)\)相邻,这个用刷表法就不行了,但是可以用类似spfa的方法转移!
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 12;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, a[maxn][maxn], cnt = 0;
int dp[maxn][maxn][1 << maxn];
struct Node
{
int x, y, s;
}pre[maxn][maxn][1 << maxn];
#define pr pair<int, int>
#define mp make_pair
queue<pr> q;
bool in[maxn][maxn];
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
In void spfa(int s)
{
while(!q.empty())
{
int x = q.front().first, y = q.front().second;
q.pop(); in[x][y] = 0;
for(int i = 0, tp; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if(!nx || nx > n || !ny || ny > m) continue;
if(dp[nx][ny][s] > (tp = dp[x][y][s] + a[nx][ny]))
{
dp[nx][ny][s] = tp;
pre[nx][ny][s] = (Node){x, y, s};
if(!in[nx][ny]) q.push(mp(nx, ny)), in[nx][ny] = 1;
}
}
}
}
int vis[maxn][maxn];
In void dfs(int x, int y, int s)
{
if(!pre[x][y][s].s) return;
vis[x][y] = 1;
Node tp = pre[x][y][s];
dfs(tp.x, tp.y, tp.s);
if(tp.x == x && tp.y == y) dfs(x, y, s ^ tp.s);
}
int main()
{
n = read(), m = read();
Mem(dp, 0x3f);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
a[i][j] = read();
if(!a[i][j]) dp[i][j][(1 << (cnt++))] = 0;
}
for(int s = 0; s < (1 << cnt); ++s)
{
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
for(int k = s, tp; k; k = s & (k - 1))
if(dp[i][j][s] > (tp = dp[i][j][k] + dp[i][j][s ^ k] - a[i][j]))
{
dp[i][j][s] = tp;
pre[i][j][s] = (Node){i, j, k};
}
if(dp[i][j][s] ^ INF) q.push(mp(i, j)), in[i][j] = 1;
}
spfa(s);
}
int x, y;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) if(!a[i][j]) {x = i, y = j; break;}
write(dp[x][y][(1 << cnt) - 1]), enter;
dfs(x, y, (1 << cnt) - 1);
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
if(!a[i][j]) putchar('x');
else putchar(vis[i][j] ? 'o' : '_');
enter;
}
return 0;
}
以上是关于[WC2008]游览计划的主要内容,如果未能解决你的问题,请参考以下文章