【“盛大遊戲杯”第15屆上海大學程序設計聯賽 L】【狀壓DP 枚舉子集 + 分塊預處理】零件組裝

零件組裝

發佈時間: 2017年7月9日 18:17   最後更新: 2017年7月9日 21:04   時間限制: 1000ms   內存限制: 128M

現有n個零件,小Y花費了很多時間來收集它們,現在他想把零件拼在一起,拼完就可以召喚神龍了。已知零件之間存在相鄰的關係,擁有相鄰關係的零件在最終的組裝結果中就是相鄰的,並且組裝過程中每次只能通過相鄰關係來組合零件。小Y每次可以選擇兩個零件(也可以是兩個零件塊,或一個零件與一個零件塊)拼起來,成爲一個零件塊,但要求拼接時必須在兩個零件塊(或零件)之間存在相鄰的零件。除此之外這些零件兩兩之間有類似於磁力的排斥關係,當將兩個零件或者零件塊拼接在一起的時候,會受到兩邊的零件間的排斥力,排斥力的大小=兩邊零件的相互排斥對數*單側零件個數的最大值(拼接完成的零件組合體中的零件之間排斥不計)。現在已知零件間的相鄰關係和排斥關係,小Y自然想知道如何拼接不費力,因此需要求出將這些零件組裝起來的最優方案,使得所有步驟的排斥力之和最小。

第一行有一個整數T表示數據組數。(T<=20
接着有T組數據,每組數據第一行是整數n表示零件個數。
接着依此有兩個nn的矩陣,都只由01構成。(2<=n<=14)
其中第一個矩陣表示零件兩兩之間的相鄰關係,第i行第j列爲1表示第i個零件與第j個零件相鄰,
第二個矩陣表示零件兩兩之間的排斥關係,第i行第j列爲1表示第i個零件與第j個零件排斥。
數據保證矩陣根據對角線對稱,並保證通過零件的相鄰關係可以最終拼接完成。

每組輸入一個整數表示拼接過程的最小排斥力之和。

1
4
0 0 1 1
0 0 1 0
1 1 0 0
1 0 0 0
0 1 0 1
1 0 1 1
0 1 0 0
1 1 0 0
6

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 14, M = (1 << 14) + 2, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int con[N][N];
int cost[N][N];
int num2[M];
int f[M];
int n;
int num[M];
int a[M][14];
int VAL(int x, int y)
{
	bool isCon = 0;
	int val = 0;
	for (int i = 0; i < num[x]; ++i) 
	{
		int xx = a[x][i];
		for (int j = 0; j < num[y]; ++j)
		{
			int yy = a[y][j];
			isCon |= con[xx][yy];
			val += cost[xx][yy];
		}
	}
	if (!isCon)return inf;
	return val * max(num[x], num[y]);
}
int main()
{
	for (int i = 1; i <= (1 << 14); ++i)
	{
		num2[i] = num2[i >> 1] + (i & 1);
		for (int j = 0; j < 14; ++j)if (i >> j & 1)
		{
			a[i][num[i]++] = j;
		}
	}
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				scanf("%d", &con[i][j]);
			}
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				scanf("%d", &cost[i][j]);
			}
		}
		MS(f, 63); f[0] = 0;
		int top = (1 << n) - 1;
		for (int i = 1; i <= top; ++i)
		{
			if (num2[i] == 1)
			{
				f[i] = 0;
				continue;
			}
			for (int j = i - 1 & i; j; j = j - 1 & i)
			{
				int k = i ^ j;
				if(f[j] != inf && f[k] != inf)//不要忽視可能會溢出的特殊情況
					gmin(f[i], f[j] + f[k] + VAL(j, k));
			}
		}
		printf("%d\n", f[top]);
	}
	return 0;
}
/*
【trick&&吐槽】
分塊真是個好東西

【題意】
http://acmoj.shu.edu.cn/problem/420/

【分析】
這題可以通過枚舉子集(複雜度爲3^n)的方法,知道合併的兩方,
if(f[j] != inf && f[k] != inf)
gmin(f[i], f[j] + f[k] + VAL(j, k));
但是這裏VAL(j, k)會消耗我們n^2級別的運算量。

【時間複雜度&&優化】
O(3^n * n * n)

*/


【“盛大遊戲杯”第15屆上海大學程序設計聯賽 L】【狀壓DP 枚舉子集 + 分塊預處理 分塊做法】零件組裝

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 14, M = 1 << 14, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int CON[N][N];
int COST[N][N];
int num2[M];					//num2[mask]表示mask的二進制表示k中有多少個1
int rev2[M];					//rev2[mask]表示mask(2^k)的k是多少
int p_con[2][2][N][M];
int p_cost[2][2][N][M];
int con[2][2][1 << 7][1 << 7];
int cost[2][2][1 << 7][1 << 7];
int f[M];
int n, h, top;
int val(int x, int tp)
{
	if(tp == 0)
		return x & top;
	else
		return x >> h;
}
int VAL(int x, int y)
{
	bool isCon = 0;
	int ret = 0;
	for (int k = 0; k < 2; ++k)
	{
		for (int u = 0; u < 2; ++u)
		{
			isCon |= con[k][u][val(x, k)][val(y, u)];
			ret += cost[k][u][val(x, k)][val(y, u)];
		}
	}
	if (!isCon)return inf;
	else return ret * max(num2[x], num2[y]);
}
int main()
{
	for (int i = 1; i < (1 << 14); ++i)
		num2[i] = num2[i >> 1] + (i & 1);
	for (int i = 0; i < 14; ++i)rev2[1 << i] = i;

	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		h = (n + 1) / 2;
		top = (1 << h) - 1;
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				scanf("%d", &CON[i][j]);
			}
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				scanf("%d", &COST[i][j]);
			}
		}

		MS(p_con, 0);
		MS(p_cost, 0);
		for (int x = 0; x < h; ++x)
		{
			for (int j = 1; j <= top; ++j)
			{
				for (int y = 0; y < h; ++y)if (j >> y & 1)
				{
					for (int k = 0; k < 2; ++k)
					{
						for (int u = 0; u < 2; ++u)
						{
							p_con[k][u][x][j] |= CON[x + k * h][y + u * h];
							p_cost[k][u][x][j] += COST[x + k * h][y + u * h];
						}
					}
				}
			}
		}

		for (int i = 1; i <= top; ++i)
		{
			int x = rev2[i & -i];
			for (int j = 1; j <= top; ++j)
			{
				for (int k = 0; k < 2; ++k)
				{
					for (int u = 0; u < 2; ++u)
					{
						con[k][u][i][j] = con[k][u][i ^ 1 << x][j] | p_con[k][u][x][j];
						cost[k][u][i][j] = cost[k][u][i ^ 1 << x][j] + p_cost[k][u][x][j];
					}
				}
			}
		}

		MS(f, 63); f[0] = 0;
		int top = (1 << n) - 1;
		for (int i = 1; i <= top; ++i)
		{
			if (num2[i] == 1)
			{
				f[i] = 0;
				continue;
			}
			for (int j = i - 1 & i; j; j = j - 1 & i)
			{
				int k = i ^ j;
				if (f[j] != inf && f[k] != inf)//不要忽視可能會溢出的特殊情況
					gmin(f[i], f[j] + f[k] + VAL(j, k));
			}
		}
		printf("%d\n", f[top]);
	}
	return 0;
}
/*
【trick&&吐槽】
分塊真是個好東西

【題意】
http://acmoj.shu.edu.cn/problem/420/

【分析】
這題可以通過枚舉子集(複雜度爲3^n)的方法,知道合併的兩方,
if(f[j] != inf && f[k] != inf)
gmin(f[i], f[j] + f[k] + VAL(j, k));
但是這裏VAL(j, k)會消耗我們n^2級別的運算量。

而如果我們分塊預處理,就是預處理出
v[2][2][2^7][2^7]這樣一個數組

比如v[0][1][5][16]就表示——
前一半是5,後一半是16<<7
這兩部分狀態之間的碰撞程度

我們這裏是用分塊預處理的,進而做到3^n*4的複雜度

【時間複雜度&&優化】
O(3^n * 4)

*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章