zoukankan      html  css  js  c++  java
  • [WC2008]游览计划

    嘟嘟嘟


    都说这题是斯坦纳树的板儿题。


    斯坦纳树,我也不知道为啥起这么个名儿,斯坦纳树主要用来解决这样一类问题:带边权无向图上有几个(一般约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;	
    }
    
  • 相关阅读:
    2019-4-16-C#-使用反射获取私有属性的方法
    2019-5-21-C#-命令行如何静默调用-del-删除文件
    2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...
    2019-9-11-完整的-P2P-应用需要包含哪些功能
    2019-3-1-VisualStudio-扩展开发-获得输出窗口内容
    2018-10-19-C#-序列类为-xml-可以使用的特性大全
    2018-8-10-win10-uwp-手把手教你使用-asp-dotnet-core-做-cs-程序
    2018-10-31-win10-uwp-使用-asp-dotnet-core-做图床服务器客户端
    2018-8-10-docfx-做一个和微软一样的文档平台
    linux分区方案
  • 原文地址:https://www.cnblogs.com/mrclr/p/10783989.html
Copyright © 2011-2022 走看看