社区讨论

求助,官方数据全过,民间几乎全没过。。。

P8817[CSP-S 2022] 假期计划参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lo7ll1ak
此快照首次捕获于
2023/10/27 03:48
2 年前
此快照最后确认于
2023/10/27 03:48
2 年前
查看原帖
用的是枚举bc预处理ad,帮忙看看为什么
CPP
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 2510, M = 2e4 + 10;

int n, m, k;
int h[N], e[M], ne[M], idx;
LL w[N];
bool st[N];
int q[M];
int dist[N][N];
int id[N][5];
LL res;

int read(){
	
	int x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

void add(int a, int b){
	
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void bfs(int s){
	
	memset(st, 0, sizeof st);
	int hh = 0, tt = 0;
	q[0] = s;
	dist[s][s] = -1;
	
	while(hh <= tt){
		
		int t = q[hh ++];
		if(st[t]) continue;
		st[t] = true;
		for(int i = h[t]; ~i; i = ne[i]){
			
			int j = e[i];
			
			if(dist[s][j] > dist[s][t] + 1){
				
				dist[s][j] = dist[s][t] + 1;
				q[++ tt] = j;
			}
		}
	}
}

int main(){
	
	n = read(), m = read(), k = read();
	
	memset(h, -1, sizeof h);
	
	for(int i = 2; i <= n; i ++) w[i] = read();
	
	while(m --){
		
		int a = read(), b = read();
		
		add(a, b), add(b, a);
	}
	
	memset(dist, 0x3f, sizeof dist);
	for(int i = 1; i <= n; i ++) bfs(i);
	
	memset(st, 0, sizeof st);
	for(int i = 2; i <= n; i ++){
		
		for(int j = 2; j <= n; j ++){
			
			if(i != j && dist[1][i] <= k && dist[i][j] <= k){
				
				st[j] = true;
				
				if(w[i] > w[id[j][1]]){
					
					id[j][3] = id[j][2];
					id[j][2] = id[j][1];
					id[j][1] = i; 
				}
				else if(w[i] > w[id[j][2]]){
					
					id[j][3] = id[j][2];
					id[j][2] = i;
				}
				else if(w[i] > w[id[j][3]]){
					
					id[j][3] = i;
				}
			}
		}
	}
	
	for(int b = 2; b <= n; b ++){
		
		for(int c = 2; c <= n; c ++){
			
			if(st[b] && st[c] && b != c && dist[b][c] <= k){
				
				for(int i = 1; i <= 3; i ++){
					
					for(int j = 1; j <= 3; j ++){
						
						int a = id[b][i], d = id[c][j];
						
						if(a != c && a != d && d != b && a && d){
							
							res = max(res, w[a] + w[b] + w[c] + w[d]);
						}
					}
				}
			}
		}
	}
	
	printf("%lld\n", res);
	
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...