85re,求助
查看原帖
85re,求助
483058
陈泽涵爱g编程楼主2023/9/27 13:55
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int n, m, k;
const int maxn = 2501;
int win[maxn], numk[maxn][maxn];
vector<int>g[maxn];
vector<int>g2[maxn];
queue<int>q;
void bfs1(int x) {
	numk[x][x] = -1;
	q.push(x);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if(numk[x][v] > numk[x][u] + 1) {
				numk[x][v] = numk[x][u] + 1;
				q.push(v); 
			}
		}
	}
	return ;
}
struct node {
	int u, get, step;
	vector<int>p;
};
int vis[maxn][5];
queue<node>qq;
int bfs2() {
	int ans = 0;
	qq.push({1, 0, 0});
	vis[1][0] =  0;
	while(!qq.empty()) {
		int u = qq.front().u, get = qq.front().get, step = qq.front().step;
		vector<int>p = qq.front().p;
		qq.pop();
//		cout << u;
		for(int i  = 0; i < g2[u].size(); i++) {
			vector<int> pp;
			pp = p;
			int flag = 0;
			int v = g2[u][i];
			for(int j = 0; j < pp.size(); j++) {
				if(v == pp[j]) flag = 1;
			}
			if(flag == 1) continue;
			if(v == 1 && step == 4) {
				ans = max(ans, get);
			} else if(v == 1 || step >= 4) {
				continue;
			} else if(vis[v][step + 1] < get + win[v]){
//				cout << v << ' ' << step + 1 << ' ' << vis[v][step] << '\n'; 
				vis[v][step + 1] = get + win[v];
				pp.push_back(v); 
				qq.push({v, get + win[v], step + 1, pp});
			}
		} 
	}
	return ans;
}
int main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
	cin >> n >> m >> k;
	for(int i = 2; i <= n; i++) {
		cin >> win[i];
	}
	for(int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			numk[i][j] = 1e9;
		}
	}
	for(int i = 1; i <= n; i++)
		bfs1(i);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
//			cout << numk[i][j] << ' ';
			if(numk[i][j] <= k) {
				g2[i].push_back(j);
			}
		}
//		cout << '\n';
	}
	cout << bfs2();
		return 0;
}
2023/9/27 13:55
加载中...