CSP-S 模拟 19/11/14

在这里插入图片描述
n ≤ 501 , k ≤ 10 n\le 501,k\le 10 n501,k10
考虑到最后结束的位置一定是一个 T T T,迪杰预处理两个宝藏直接的最短路状压 d p dp dp 即可

#include<bits/stdc++.h>
#define cs const
using namespace std;
typedef long long ll;
int read(){
	int cnt = 0, f = 1; char ch = 0;
	while(!isdigit(ch)){ ch = getchar(); if(ch == '-') f = -1; }
	while(isdigit(ch)) cnt = cnt*10 + (ch-'0'), ch = getchar();
	return cnt * f;
}
cs int N = 505, M = 1 << 10 | 5, K = 15;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
ll Mx(ll &a, ll b){ if(a < b) a = b;}
int n, k, T, Lx, Rx;
int R, G, B;
ll f[K][M], d[K][K];
char s[N];
int sx, sy, ct;
int bin[M], vl[N][N];
typedef pair<int,int> pi;
pi ps[K];
#define mp make_pair
ll dis[K][N][N]; bool vis[N][N];
struct node{ 
	int x, y; ll d; 
	node(int _x = 0, int _y = 0, ll _d = 0){x = _x, y = _y, d = _d;}
	bool operator < (cs node &a) cs{ return d > a.d;} 
};

void Dij(int stx, int sty, int opt){
	memset(dis[opt], 0x3f, sizeof(dis[opt]));
	memset(vis, 0, sizeof(vis));
	priority_queue<node> q; q.push(node(stx, sty, 0));
	dis[opt][stx][sty] = 0;
	while(!q.empty()){
		node u = q.top(); q.pop(); 
		if(vis[u.x][u.y]) continue; vis[u.x][u.y] = true;
		for(int i = 0; i < 4; i++){
			int x = u.x + dx[i], y = u.y + dy[i];
			if(x < 1 || x > n || y < 1 || y > n) continue;
			if(dis[opt][x][y] > dis[opt][u.x][u.y] + (ll)vl[x][y]){
				dis[opt][x][y] = dis[opt][u.x][u.y] + (ll)vl[x][y];
				q.push(node(x, y, dis[opt][x][y]));
			}
		} 
	} 
}
ll Solve(){
	Dij(sx, sy, 0);
	memset(f, -0x3f, sizeof(f));
	for(int i = 1; i <= ct; i++) 
		f[i][1 << i-1] = T - dis[0][ps[i].first][ps[i].second];
	for(int i = 1; i <= ct; i++){
		Dij(ps[i].first, ps[i].second, i);
		for(int j = 1; j <= ct; j++){
			d[i][j] = - dis[i][ps[j].first][ps[j].second];
		}
	}
	int S = (1 << k) - 1;
	for(int i = 1; i <= S; i++){
		for(int j = 1; j <= k; j++){
			if(!((i >> (j - 1)) & 1)) continue;
			for(int l = 1; l <= k; l++){
				if((i >> (l - 1)) & 1) continue;
				Mx(f[l][i | (1 << l - 1)], f[j][i] + d[j][l] + (ll)T);
			}
		}
	}
	ll ans = - 1e15;
	for(int i = 1; i <= S; i++) if(bin[i] >= Lx && bin[i] <= Rx){
		for(int j = 1; j <= k; j++) Mx(ans, f[j][i]);
	} return ans;
}
int main(){
	freopen("coin.in","r",stdin);
	freopen("coin.out","w",stdout);
	n = read(), k = read(), T = read(), Lx = read(), Rx = read();
	R = read(), G = read(), B = read();
	for(int i = 1; i < M; i++) bin[i] = bin[i >> 1] + (i&1);
	for(int i = 1; i <= n; i++){
		scanf("%s", s + 1);
		for(int j = 1; j <= n; j++){
			if(s[j] == 'R') vl[i][j] = R;
			if(s[j] == 'B') vl[i][j] = B;
			if(s[j] == 'G') vl[i][j] = G;
			if(s[j] == 'T') ps[++ct] = mp(i, j);
			if(s[j] == 'S') sx = i, sy = j;
		} 
	} cout << Solve(); return 0;
}

在这里插入图片描述
从每个叶子开始 d f s dfs dfs,只讨论从上到下的路径就可以把所有路径考虑完
发现并不能从什么数据结构之类的角度入手,只能从 g c d gcd gcd 的角度入手
一个点到上面的一个点, g c d gcd gcd 要么不变,要么至少减小一半
那么如果我们把 g c d gcd gcd 相同的缩成一段,最后的段数是 l o g V logV logV 级别的
于是每个点暴力开一个 l o g log log 大小的数组存这样的一段一段的值
查询的时候一定是查当前 g c d gcd gcd 的最上面的那一个,于是保留最上面的值即可
加入一个,暴力与前面的取 g c d gcd gcd 并合并同类项

#include<bits/stdc++.h>
#define cs const
using namespace std;
typedef long long ll;
int read(){
	int cnt = 0, f = 1; char ch = 0;
	while(!isdigit(ch)){ ch = getchar(); if(ch == '-') f = -1; }
	while(isdigit(ch)) cnt = cnt*10 + (ch-'0'), ch = getchar();
	return cnt * f;
}
cs int N = 2e5 + 5, M = N << 1;
int n, vl[N], du[N]; ll ans = 0;
int first[N], nxt[M], to[M], tot;
void add(int x, int y){ nxt[++tot] = first[x], first[x] = tot, to[tot] = y; }
int gcd(int a, int b){ return !b ? a : gcd(b, a % b); }
typedef pair<int,int> pi;
#define mp make_pair
struct Block{ pi a[35]; int ct; }t[N];
int dep[N];
void dfs(int u, int fa){
	t[u] = t[fa]; dep[u] = dep[fa] + 1;
	t[u].a[++t[u].ct] = mp(u, vl[u]);
	for(int i = 1; i <= t[u].ct; i++){
		t[u].a[i] = mp(t[u].a[i].first, gcd(t[u].a[i].second, vl[u]));
		ans = max(ans, 1ll * t[u].a[i].second * (dep[u] - dep[t[u].a[i].first] + 1));
	} 
	int ct = 0;
	for(int i = 1; i <= t[u].ct; i++) 
		if(t[u].a[i].second != t[u].a[i - 1].second) 
			t[u].a[++ct] = t[u].a[i];
	t[u].ct = ct;
	for(int i = first[u]; i; i = nxt[i]) if(to[i] ^ fa) dfs(to[i], u);
}
int main(){
	n = read();
	for(int i = 1; i <= n; i++) vl[i] = read();
	for(int i = 1; i < n; i++){
		int x = read(), y = read();
		add(x, y); add(y, x); ++du[x]; ++du[y];
	}
	for(int i = 1; i <= n; i++){
		if(du[i] == 1) dfs(i, 0);
	} cout << ans; return 0;
}

T3:有一个绝妙的方法但是这里写不下
传送门

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值