QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#878 | #580913 | #9374. Escape | stavage | obliviatecqupt | Failed. | 2024-09-22 09:42:32 | 2024-09-22 09:42:33 |
详细
Extra Test:
Accepted
time: 0ms
memory: 3604kb
input:
1 10 10 5 1 9 4 6 1 2 7 9 1 8 2 3 4 10 3 7 1 4 1 5 1 6
output:
7 1 9 7 3 2 1 4 10
result:
ok Accepted! (1 test case)
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#580913 | #9374. Escape | obliviatecqupt | AC ✓ | 543ms | 59252kb | C++20 | 2.0kb | 2024-09-22 00:56:01 | 2024-09-22 00:56:05 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
typedef pair<int, int> pii;
void solve(){
int n, m, D;
cin >> n >> m >> D;
vector<vector<int>> e(n+1);
while(m--){
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
int k;
cin >> k;
set<int> s;
while(k--){
int x;
cin >> x;
s.insert(x);
}
vector<vector<int>> rd(n+1, vector<int>(2, 1e18));
queue<pair<int,int>> q;
for(int x : s){
rd[x][0] = 0;
q.push({0, x});
}
while(q.size()){
auto [distx, x] = q.front();
q.pop();
// printf("%d : %d\n", distx, x);
int disty = distx + 1;
if(disty > D) continue;
for(int y : e[x]){
if(rd[y][disty&1] > disty){
rd[y][disty&1] = disty;
q.push({disty, y});
}
}
}
vector<vector<int>> d(n+1, vector<int>(2, 1e18));
vector<vector<pair<int,int>>> pre(n+1, vector<pair<int,int>>(2, {0, 0}));
d[1][0] = 0;
q.push({0, 1});
while(q.size()){
auto [distx, x] = q.front();
q.pop();
int disty = distx + 1;
for(int y : e[x]){
if(rd[y][disty&1] <= disty) continue;
// int t = rd[y][(disty&1)^1] - 2;
// if(t > 0 && t <= disty) continue;
if(d[y][disty&1] != 1e18) continue;
d[y][disty&1] = disty;
pre[y][disty&1] = {x, distx&1};
q.push({d[y][disty&1], y});
}
}
// for(int i = 1; i <= n; i++){
// printf("%lld : %lld, %lld\n", i, rd[i][0], rd[i][1]);
// }
if(min(d[n][0], d[n][1]) == 1e18){
cout << "-1" << '\n';
}else{
vector<int> ans;
int curid = n, curdist = min(d[n][0], d[n][1]);
while(curid != 0){
ans.push_back(curid);
curid = pre[curid][curdist&1ll].first;
curdist--;
}
reverse(ans.begin(), ans.end());
cout << ans.size() - 1 << '\n';
for(int x : ans) cout << x << ' ';
cout << '\n';
}
// printf("\n");
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _;
cin >> _;
while(_--) solve();
return 0;
}