QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#300160 | #6306. Chase Game | LittleXi | WA | 0ms | 3764kb | C++17 | 3.1kb | 2024-01-07 19:16:05 | 2024-01-07 19:16:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
ll n,m,k,d;
vector<vector<ll>> adj;
vector<ll> cau(ll st){
vector<ll> re(n,0);
vector<ll> vis(n,0);
vis[st] = 1;
queue<pair<ll,ll>> q;
q.push({st,0});
while(q.size()){
auto&[x,dis] = q.front();q.pop();
re[x] = dis;
// cout<<x<<" "<<dis<<endl;
for(auto to:adj[x]){
if(vis[to]) continue;
vis[to] = 1;
q.push({to,dis+1});
}
}
return re;
}
vector<ll> f;
void init(){
f = vector<ll> (n,0);
f[0] = d;
ll dis = 0;
for(ll i =1;i<n;i++){
dis++;
if(dis==d) dis = 0;
f[i] = f[i-1]+d-dis;
}
// for(ll x:f) cout<<x<<" ";
// cout<<endl;
}
void solve(){
cin>>n>>m>>k>>d;
k--;
adj = vector<vector<ll>> (n);
for(ll i=0;i<m;i++){
ll x,y;cin>>x>>y;x--;y--;
adj[x].push_back(y);adj[y].push_back(x);
}
auto at_dis = cau(k);
auto over_dis =cau(n-1);
init();
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
vector<ll> vis(n,0);vis[0] = 1;
ll ans = f[over_dis[0]-1];
for(ll to:adj[0]){
if(at_dis[to]>=d){
ans = min(ans,f[over_dis[to]]);
continue;
}else{
pq.push({d-at_dis[to],to});
}
}
while(pq.size()){
while(pq.size()&&vis[pq.top().second]) pq.pop();
if(pq.size()==0) break;
auto p = pq.top();pq.pop();
ll cost = p.first,x =p.second;
vis[x] = 1;
// cout<<"x:"<<x<<" cost:"<<cost<<endl;
// if(x==8&&cost==4){
// while(pq.size()) cout<<pq.top().first<<" "<<pq.top().second<<endl,pq.pop();
// return;
// }
for(auto to:adj[x]){
if(at_dis[to]>=d){
ans = min(ans,cost+f[over_dis[to]]);
continue;
}
if(vis[to]) continue;
// cout<<"to:"<< to<<" "<<cost+d-at_dis[to]<<endl;
pq.push({cost+d-at_dis[to],to});
// vis[to] = 1;
}
}
cout<<ans<<endl;
}
/*
5 5 2 2
1 2
2 4
4 5
1 3
3 5
5 4 1 3
1 2
2 3
3 4
4 5
*/
signed main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
ll t = 1;
// cin>>t;
while(t--)
solve();
}
/*
vector<ll> dp(n,1e18);
vector<ll> vis(n,0);
queue<ll> q;
q.push(0);vis[0] = 1;dp[0]=0;
ll ans = f[over_dis[0]];
while(q.size()){
ll x = q.front();q.pop();
if(x!=0&&at_dis[x] >= d){
ans = min(ans,dp[x]+f[over_dis[x]]);
continue;
}
if(x==n-1){
ans = min(ans,dp[n-1]);
continue;
}
for(auto to:adj[x]){
if(vis[to]) continue;
if(at_dis[to]<d) dp[to] = min(dp[to],dp[x]+d-at_dis[to]);
else dp[to] = min(dp[to],dp[x]);
// cout<<x<<" "<<to<<" "<<dp[to]<<endl;
vis[to] =1;q.push(to);
}
}
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3764kb
input:
5 5 3 1 1 2 2 4 4 5 1 3 3 5
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
13 17 12 3 1 2 2 3 3 4 4 13 5 13 7 8 7 9 7 10 7 11 7 6 12 7 1 8 8 9 9 10 10 11 11 6 6 13
output:
7
result:
ok 1 number(s): "7"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
10 9 4 1 4 3 1 2 7 6 6 5 8 9 9 10 5 4 8 7 3 2
output:
9
result:
ok 1 number(s): "9"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
10 20 9 1 9 5 2 9 4 5 10 5 7 3 1 8 7 1 7 5 3 4 3 1 7 8 3 8 9 3 10 6 9 1 1 6 8 5 6 2 1 10 2 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3740kb
input:
10 20 3 1 1 4 6 7 5 4 5 3 2 1 8 1 7 8 2 6 2 4 4 8 9 5 1 10 7 4 5 8 4 10 2 5 6 10 4 6 3 7 1 3
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
10 20 4 1 2 7 6 4 2 3 2 4 6 5 8 5 6 7 10 2 3 10 1 8 3 9 3 7 1 7 5 1 6 1 3 4 2 1 5 2 9 2 9 10
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3560kb
input:
10 20 1 10 10 9 2 5 7 8 7 10 10 8 8 9 5 6 3 1 6 4 7 9 2 3 3 6 2 1 8 6 5 8 7 4 4 3 2 4 4 1 9 6
output:
27
result:
wrong answer 1st numbers differ - expected: '24', found: '27'