动态规划——多进程DP

动态规划——多进程DP

luogu1004

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
luogu1004
多进程DP 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
void read(T& w)
{
	char r;int f=1;
	for(r=getchar();(r<48||r>57)&&r!='-';r=getchar());
	if(r=='-')r=getchar(),f=-1;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
}
const int maxn=13;
int n;
int a[maxn][maxn];
void input()
{
	read(n);
	int x,y,z;
	for(read(x),read(y),read(z);x||y||z;read(x),read(y),read(z))
	{
		a[x][y]=z;
	}
}

int f[maxn][maxn][maxn][maxn];
void dp()
{
	for(int i=1;i<=n;++i) for(int j=1;j<=n;++j)
	{
		for(int k=1;k<=n;++k) for(int l=1;l<=n;++l)
		{
			f[i][j][k][l]
			=Max(
				Max(
					f[i-1][j][k-1][l],f[i-1][j][k][l-1]),
				Max(
					f[i][j-1][k-1][l],f[i][j-1][k][l-1])
				);
			f[i][j][k][l]+=a[i][j];
			if(i!=k||j!=l)
				f[i][j][k][l]+=a[k][l];
			//cout<<f[i][j][k][l]<<" ";
		}
	}
}
int main()
{
	freopen("luogu1004_1.in","r",stdin);
	//freopen("luogu1004.out","w",stdout);
	input();
	dp();
	printf("%d\n",f[n][n][n][n]);
	return 0;
}

luogu1006

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
luogu1006
多进程DP 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
void read(T& w)
{
	char r;int f=1;
	for(r=getchar();(r<48||r>57)&&r!='-';r=getchar());
	if(r=='-')r=getchar(),f=-1;
	for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
	w*=f;
}
const int maxn=53;
int n,m;
int a[maxn][maxn];
void input()
{
	read(n),read(m);
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			read(a[i][j]);
		}
	}
}

int f[maxn][maxn][maxn][maxn];
void dp()
{
	for(int i=1;i<=n;++i) for(int j=1;j<=m;++j)
	{
		for(int k=1;k<=n;++k) for(int l=1;l<=m;++l)
		{
			f[i][j][k][l]
			=Max(
				Max(
					f[i-1][j][k-1][l],f[i-1][j][k][l-1]),
				Max(
					f[i][j-1][k-1][l],f[i][j-1][k][l-1])
				);
			f[i][j][k][l]+=a[i][j];
			if(i!=k||j!=l)
				f[i][j][k][l]+=a[k][l];
			//cout<<f[i][j][k][l]<<" ";
		}
	}
}
int main()
{
	freopen("luogu1006_1.in","r",stdin);
	//freopen("luogu1006.out","w",stdout);	
	input();
	dp();
	printf("%d\n",f[n][m][n][m]);
	return 0;
}
0%