思路:一道模擬題,參考了大佬們的代碼,目前嘗試了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;
}