QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303019 | #6306. Chase Game | ZYF_B# | WA | 3ms | 12060kb | C++14 | 1.8kb | 2024-01-11 16:55:47 | 2024-01-11 16:55:49 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define re register
using namespace std;
inline int read()
{
int f=1,x=0;char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-1;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return f*x;
}
const int N=2e5+5;
vector<int> e[N];
int disk[N],disn[N];
ll dis[N];
queue<int> q;
int vis[N];
int n,m,k,d;
const ll INF=1ll<<60;
void bfs(int s,int *dis)
{
for(int i=1;i<=n;i++) vis[i]=0;
for(int i=1;i<=n;i++) dis[i]=1e9;
dis[s]=0,vis[s]=1;
q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();
for(int y:e[x])
{
if(vis[y]) continue;
vis[y]=1;
dis[y]=dis[x]+1;
q.push(y);
}
}
}
struct node
{
int pos;
ll val;
friend bool operator < (node a,node b)
{
return a.val>b.val;
}
};
priority_queue<node> Q;
void solve()
{
ll ans=INF;
for(int i=1;i<=n;i++) vis[i]=0;
for(int i=1;i<=n;i++) dis[i]=INF;
Q.push({1,0});
dis[1]=0;
while(!Q.empty())
{
auto res=Q.top();Q.pop();
int x=res.pos;
if(vis[x]) continue;
vis[x]=1;
// cout<<x<<" "<<val<<endl;
for(int y:e[x])
{
if(disk[y]>=d)
{
// cout<<y<<" "<<x<<" "<< dis[x]<<endl;
ll sum=dis[x]+d-disk[x];
int len=disn[y]+1;
int t=len/d,b=len%d;
sum+=1ll*t*d*(d+1)/2;
sum+=1ll*(d+d-b+1)*b/2;
// cout<<sum<<endl;
ans=min(ans,sum);
continue;
}
if(dis[y]>dis[x]+d-disk[x])
{
dis[y]=dis[x]+d-disk[x];
Q.push({y,dis[y]});
}
}
}
printf("%lld\n",min(ans,dis[n]));
}
int main()
{
n=read(),m=read(),k=read(),d=read();
for(int i=1;i<=m;i++)
{
int a=read(),b=read();
e[a].push_back(b);
e[b].push_back(a);
}
bfs(k,disk);
bfs(n,disn);
// for(int i=1;i<=n;i++) cout<<disk[i]<<endl;
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 11748kb
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: 12060kb
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: -100
Wrong Answer
time: 2ms
memory: 10860kb
input:
10 9 4 1 4 3 1 2 7 6 6 5 8 9 9 10 5 4 8 7 3 2
output:
7
result:
wrong answer 1st numbers differ - expected: '9', found: '7'