嘟嘟嘟
都说这题是斯坦纳树的板儿题。
斯坦纳树,我也不知道为啥起这么个名儿,斯坦纳树主要用来解决这样一类问题:带边权无向图上有几个(一般约10个)点是【关键点】,要求选择一些边使这些点在同一个联通块内,同时要求所选的边的边权和最小。(摘自兔哥博客)
但说白了就是一种状压dp。令(dp[i][j][S])表示和点((i, j))相连的关键点的状态为(S)时的最小代价,于是有两个转移方程:
[egin{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<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
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');
}
In void MYFILE()
{
#ifndef mrclr
freopen("ha.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n, m, N, a[maxn][maxn], cnt = 0;
struct Node {int x, y, S;} pre[maxn][maxn][1 << maxn];
int dp[maxn][maxn][1 << maxn];
#define pr pair<int, int>
#define mp make_pair
#define fir first
#define sec second
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
bool in[maxn][maxn];
queue<pr> q;
In void spfa(int S)
{
while(!q.empty())
{
int x = q.front().fir, y = q.front().sec;
q.pop(); in[x][y] = 0;
for(int i = 0; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if(nx <= 0 || nx > n || ny <= 0 || ny > m) continue;
if(dp[nx][ny][S] > dp[x][y][S] + a[nx][ny])
{
dp[nx][ny][S] = dp[x][y][S] + a[nx][ny];
pre[nx][ny][S] = (Node){x, y, S};
if(!in[nx][ny]) q.push(mp(nx, ny)), in[nx][ny] = 1;
}
}
}
}
bool vis[maxn][maxn];
In void dfs(int x, int y, int S)
{
vis[x][y] = 1; Node tp = pre[x][y][S];
if(!tp.x) return;
dfs(tp.x, tp.y, tp.S);
if(tp.x == x && tp.y == y) dfs(tp.x, tp.y, S ^ tp.S);
}
int main()
{
// MYFILE();
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;
}
N = 1 << cnt;
for(int S = 0; S < N; ++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][S ^ k] + dp[i][j][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, ans = INF;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) if(dp[i][j][N - 1] < ans) ans = dp[i][j][N - 1], x = i, y = j;
write(ans), enter;
dfs(x, y, (1 << cnt) - 1);
for(int i = 1; i <= n; ++i, enter)
for(int j = 1; j <= m; ++j)
{
if(!a[i][j]) putchar('x');
else putchar(vis[i][j] ? 'o' : '_');
}
return 0;
}