其他分享
首页 > 其他分享> > CSP2021 考前复习(所有模板)

CSP2021 考前复习(所有模板)

作者:互联网

一、\(\rm dp\)

1. \(01\) 背包

P1048 [NOIP2005 普及组] 采药

#include <iostream>
#include <cstdio>
using namespace std;

const int MAXM = 105;
const int MAXT = 1005;

int v[MAXM], w[MAXM];
int dp[MAXT];

int main()
{
	int t, m;
	scanf("%d%d", &t, &m);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d%d", v + i, w + i);
	}
	for (int i = 1; i <= m; i++)
	{
		for (int j = t; j >= v[i]; j--)
		{
			dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
		}
	}
	printf("%d\n", dp[t]);
	return 0;
}

\(2.\) 完全背包

P1616 疯狂的采药

tips:开long long

#include <iostream>
#include <cstdio>
#define int long long
using namespace std;

const int MAXM = 1e4 + 5;
const int MAXT = 1e7 + 5;

int v[MAXM], w[MAXM];
int dp[MAXT];

signed main()
{
	int t, m;
	scanf("%lld%lld", &t, &m);
	for (int i = 1; i <= m; i++)
	{
		scanf("%lld%lld", v + i, w + i);
	}
	for (int i = 1; i <= m; i++)
	{
		for (int j = v[i]; j <= t; j++)
		{
			dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
		}
	}
	printf("%lld\n", dp[t]);
	return 0;
}

\(3.\) 多重背包(二进制优化)

P1776 宝物筛选

#include <iostream>
#include <cstdio>
using namespace std;

const int MAXN = 1e5 + 5;
const int MAXT = 4e4 + 5;

int v[MAXN], w[MAXN];
int dp[MAXT];

int main()
{
	int n, t, tot = 0;
	scanf("%d%d", &n, &t);
	for (int i = 1; i <= n; i++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		for (int j = 1; j <= c; j <<= 1)
		{
			c -= j;
			w[++tot] = a * j;
			v[tot] = b * j;
		}
		if (c)
		{
			w[++tot] = a * c;
			v[tot] = b * c;
		}
	}
	for (int i = 1; i <= tot; i++)
	{
		for (int j = t; j >= v[i]; j--)
		{
			dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
		}
	}
	printf("%d\n", dp[t]);
	return 0;
}

\(4.\) 区间 \(\rm dp\)(环形类问题)

P1880 [NOI1995] 石子合并

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 205;

int a[MAXN], sum[MAXN], dp1[MAXN][MAXN], dp2[MAXN][MAXN];

int main()
{
	memset(dp1, 0x3f, sizeof(dp1));
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", a + i);
		a[i + n] = a[i];
	}
	for (int i = 1; i <= (n << 1); i++)
	{
		sum[i] = sum[i - 1] + a[i];
		dp1[i][i] = 0;
	}
	for (int len = 2; len <= n; len++)
	{
		for (int i = 1; i + len - 1 <= (n << 1); i++)
		{
			int j = i + len - 1;
			dp1[i][j] = 0x3f3f3f3f;
			for (int k = i; k < j; k++)
			{
				dp1[i][j] = min(dp1[i][j], dp1[i][k] + dp1[k + 1][j] + sum[j] - sum[i - 1]);
				dp2[i][j] = max(dp2[i][j], dp2[i][k] + dp2[k + 1][j] + sum[j] - sum[i - 1]);
			}
		}
	}
	int ans1 = 0x3f3f3f3f, ans2 = 0;
	for (int i = 1; i <= n; i++)
	{
		ans1 = min(ans1, dp1[i][i + n - 1]);
		ans2 = max(ans2, dp2[i][i + n - 1]);
	}
	printf("%d\n%d\n", ans1, ans2);
	return 0;
}

二、图论

1. 并查集(路径压缩+按秩合并)

P3367 【模板】并查集

#include <iostream>
#include <cstdio>
using namespace std;

const int MAXN = 1e4 + 5;

int n, m;
int fa[MAXN], dep[MAXN];

void init()
{
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
	}
}

int find(int x)
{
	if (x == fa[x])
	{
		return x;
	}
	return fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
	x = find(x), y = find(y);
	if (x != y)
	{
		if (dep[x] > dep[y])
		{
			fa[y] = x;
		}
		else
		{
			fa[x] = y;
			if (dep[x] == dep[y])
			{
				dep[y]++;
			}
		}
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	init();
	while (m--)
	{
		int z, x, y;
		scanf("%d%d%d", &z, &x, &y);
		if (z == 1)
		{
			merge(x, y);
		}
		else
		{
			if (find(x) == find(y))
			{
				puts("Y");
			}
			else
			{
				puts("N");
			}
		}
	}
	return 0;
}

2. 最短路

\(I.\rm dijktra\)

P4779 【模板】单源最短路径(标准版)

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int MAXN = 1e5 + 5;
const int MAXM = 2e5 + 5;

int cnt;
int head[MAXN];

struct edge
{
	int to, dis, nxt;
}e[MAXM];

void add(int u, int v, int w)
{
	e[++cnt] = edge{v, w, head[u]};
	head[u] = cnt;
}

struct que
{
	int pos, dis;
	bool operator <(const que &x)const
	{
		return x.dis < dis;
	}
};

int dis[MAXN];
bool vis[MAXN];
priority_queue<que> pq;

void dijkstra(int s)
{
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	pq.push(que{s, 0});
	while (!pq.empty())
	{
		int u = pq.top().pos;
		pq.pop();
		if (vis[u])
		{
			continue;
		}
		vis[u] = true;
		for (int i = head[u]; i; i = e[i].nxt)
		{
			int v = e[i].to, w = e[i].dis;
			if (dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				pq.push(que{v, dis[v]});
			}
		}
	}
}

int main()
{
	int n, m, s;
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		add(u, v, w);
	}
	dijkstra(s);
	for (int i = 1; i <= n; i++)
	{
		printf("%d ", dis[i]);
	}
	return 0;
}

\(II.\rm spfa\)

P3371 【模板】单源最短路径(弱化版)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int MAXN = 1e4 + 5;
const int MAXM = 5e5 + 5;

int cnt;
int head[MAXN];

struct edge
{
	int to, dis, nxt;
}e[MAXM];

void add(int u, int v, int w)
{
	e[++cnt] = edge{v, w, head[u]};
	head[u] = cnt;
}

int dis[MAXN];
bool vis[MAXN];
queue<int> q;

void spfa(int s)
{
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i; i = e[i].nxt)
		{
			int v = e[i].to, w = e[i].dis;
			if (dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				if (!vis[v])
				{
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
}

int main()
{
	int n, m, s;
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		add(u, v, w);
	}
	spfa(s);
	for (int i = 1; i <= n; i++)
	{
		if (dis[i] == 0x3f3f3f3f)
		{
			printf("2147483647 ");
		}
		else
		{
			printf("%d ", dis[i]);
		}
	}
	return 0;
}

标签:const,考前,CSP2021,int,MAXN,dp,include,模板,dis
来源: https://www.cnblogs.com/mangoworld/p/CSP2021-muban.html