ccf csp认证 201912-4 区块链

思路:一道模拟题,参考了大佬们的代码,目前尝试了bfs直接暴力,(把当前要扩展的链放到搜到的点上),80分,会超内存。
可以改成在询问时处理所有请求,并生成结果队列,能用引用的地方尽量用引用(不然很容易超时)。

还有对于题意,我刚开始有个误区,以为规则bi <= bi+1只有输入是a,b,c才有效,但是仔细读题,查询操作也是遵循这个规则的。在这里插入图片描述

代码:
80分:

#include <bits/stdc++.h>

using namespace std;

const int N = 505;

vector<vector<int>> g(505);  //存储节点的边关系
vector<vector<int>> ans(505, {0});  //存储每个节点的主链
map <int , unordered_map<int , array < vector<int> , 2> > >ac;
int t , n , m , k;

bool check(vector <int> &a ,vector <int> &b) {
	if(a.size() < b.size() || (a.size() == b.size() && a.back() > b.back())) {
		return true;
	}
	return false;
}

void update(int time , int u) {
	auto &cur = ans[u];
	for(auto it : g[u]){
		auto &tmp = ac[time][(it)][0];
		if( ( check(tmp , cur) && !tmp.empty() ) || ( check(ans[it] , cur ) && tmp.empty() ) ){
		   tmp = cur;
		}	
	}
}

void query(int time) {
    for(auto& ac_d : ac) {
    	int ctime = ac_d.first;
    	if(ctime > time)break;	 
		for(auto& ele : ac_d.second) {
			int jd = ele.first;
			auto & in = ele.second[0] , &out = ele.second[1];
			bool flag = 0;
			if(check(ans[jd] , in)) {
				ans[jd] = in;
				flag = 1;
			}
			for(int b : out) {
				ans[jd].push_back(b);
			}
			if(flag || !out.empty()) {
				update(ctime + t , jd);
			}
		}
	}
	ac.erase(ac.begin() , ac.upper_bound(time));	
} 

int main() {
	ios::sync_with_stdio(false);
	int n , m;
	cin >> n >> m;
	for(int i = 0 ; i < m ; i++) {
		int u , v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
    int k;
	cin >> t >> k;
	while(k--) {
		int a , b , c;
		cin >> a >> b;
		if(cin.get() == '\n' || cin.eof()) {
			query(b);
			cout << ans[a].size();
			for(int x : ans[a]) {
				cout << " " << x;
			}
			cout << "\n";
		}
		else {
			cin >> c;
			ac[b][a][1].push_back(c);
		}
	}
	return 0;
}

模拟法(100分):

#include <bits/stdc++.h>

using namespace std;

const int N = 505;

vector<vector<int>> g(505);  //存储节点的边关系
vector<vector<int>> ans(505, {0});  //存储每个节点的主链
map <int , unordered_map<int , array < vector<int> , 2> > >ac;
int t , n , m , k;

bool check(vector <int> &a ,vector <int> &b) {
	if(a.size() < b.size() || (a.size() == b.size() && a.back() > b.back())) {
		return true;
	}
	return false;
}

void update(int time , int u) {
	auto &cur = ans[u];
	for(auto it : g[u]){
		auto &tmp = ac[time][(it)][0];
		if( ( check(tmp , cur) && !tmp.empty() ) || ( check(ans[it] , cur ) && tmp.empty() ) ){
		   tmp = cur;
		}	
	}
}

void query(int time) {
    for(auto& ac_d : ac) {
    	int ctime = ac_d.first;
    	if(ctime > time)break;	 
		for(auto& ele : ac_d.second) {
			int jd = ele.first;
			auto & in = ele.second[0] , &out = ele.second[1];
			bool flag = 0;
			if(check(ans[jd] , in)) {
				ans[jd] = in;
				flag = 1;
			}
			for(int b : out) {
				ans[jd].push_back(b);
			}
			if(flag || !out.empty()) {
				update(ctime + t , jd);
			}
		}
	}
	ac.erase(ac.begin() , ac.upper_bound(time));	
} 

int main() {
	ios::sync_with_stdio(false);
	int n , m;
	cin >> n >> m;
	for(int i = 0 ; i < m ; i++) {
		int u , v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
    int k;
	cin >> t >> k;
	while(k--) {
		int a , b , c;
		cin >> a >> b;
		if(cin.get() == '\n' || cin.eof()) {
			query(b);
			cout << ans[a].size();
			for(int x : ans[a]) {
				cout << " " << x;
			}
			cout << "\n";
		}
		else {
			cin >> c;
			ac[b][a][1].push_back(c);
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章