QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#364705 | #5073. Elden Ring | Baiyu0123 | WA | 145ms | 23188kb | C++14 | 2.7kb | 2024-03-24 16:09:37 | 2024-03-24 16:09:38 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+1000,lim=1e9;
vector<int> ed[maxn],t[maxn],s[maxn];
struct edge {
int x,y,w;
}b[maxn];
bool cmp(const edge &x,const edge &y) {
return x.w<y.w;
}
int g[maxn],fa[maxn],siz[maxn];
queue<int> q;
int dis[maxn];
int n,m,A,B,a[maxn];
void bfs(int x) {
q.push(1);
for (int i=1;i<=n;i++) dis[i]=lim;
dis[1]=0;
while (!q.empty()) {
int u=q.front();q.pop();
if (a[1]-1ll*dis[u]*x<=0) continue;
for (int v:ed[u]) {
if (dis[v]>dis[u]+1) {
dis[v]=dis[u]+1;
q.push(v);
}
}
}
if (dis[n]==lim) cout<<"-1\n";
else cout<<dis[n]<<"\n";
}
void bfs2() {
for (int i=1;i<=n;i++) dis[i]=lim;
dis[1]=0;
for (int w=0;w<=n;w++) {
for (int u:t[w]) {
if (dis[u]<=w) {
dis[u]=w;
for (int v:ed[u]) {
if (dis[v]>dis[u]+1) {
dis[v]=dis[u]+1;
if (dis[v]>g[v]&&g[v]!=-1) q.push(v);
}
}
}
}
while (!q.empty()) {
int u=q.front();
assert(dis[u]>=w);
if (dis[u]>w) break;
q.pop();
for (int v:ed[u]) {
if (dis[v]>dis[u]+1) {
dis[v]=dis[u]+1;
if (dis[v]>g[v]&&g[v]!=-1) q.push(v);
}
}
}
t[w].clear();
}
if (dis[n]==lim) cout<<"-1\n";
else cout<<dis[n]<<"\n";
}
int find(int x) {
if (fa[x]==x) return x;
return find(fa[x]);
}
void merge(int x,int y,int w) {
int fx=find(x),fy=find(y),f1=find(1);
if (fx==fy) return ;
if (siz[fx]<siz[fy]) swap(fx,fy);
if (fx==f1) for (int u:s[fy]) g[u]=w+1;
if (fy==f1) for (int u:s[fx]) g[u]=w+1;
for (int u:s[fy]) s[fx].push_back(u);
s[fy].clear();fa[fy]=fx;siz[fx]+=siz[fy];
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;cin>>T;
while (T--) {
cin>>n>>m>>A>>B;
for (int i=1;i<=m;i++) {
int x,y;cin>>x>>y;
ed[x].push_back(y);
ed[y].push_back(x);
if (A>B) b[i]=(edge){x,y,0};
}
for (int i=1;i<=n;i++) {
cin>>a[i];
if (i!=1) a[i]+=B;
}
A-=B;
if (A<=0) {
bfs(-A);
} else {
for (int i=1;i<=m;i++) {
int x=b[i].x,y=b[i].y;
int wx=(x==1||a[x]<a[1])?0:(a[x]-a[1])/A+1;
int wy=(y==1||a[y]<a[1])?0:(a[y]-a[1])/A+1;
b[i].w=max(wx,wy);
}
sort(b+1,b+m+1,cmp);
for (int i=1;i<=n;i++) s[i].push_back(i),fa[i]=i,siz[i]=1,g[i]=-1;
g[1]=0;
for (int w=0,i=0;w<n;w++) {
if (siz[find(1)]<=w) break;
while (i+1<=m&&b[i+1].w==w) {
merge(b[i+1].x,b[i+1].y,w);
i++;
}
}
for (int u=1;u<=n;u++) {
if (g[u]!=-1) t[g[u]].push_back(u);
}
bfs2();
for (int u=1;u<=n;u++) s[u].clear();
}
for (int i=1;i<=n;i++) ed[i].clear();
}
}
/*
39991723655814713848046032694137883815354365954917
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22532kb
input:
2 5 4 5 8 1 2 1 3 1 4 4 5 15 1 1 1 1 5 4 10 5 1 2 1 3 1 4 4 5 10 4 4 4 19
output:
2 4
result:
ok 2 number(s): "2 4"
Test #2:
score: -100
Wrong Answer
time: 145ms
memory: 23188kb
input:
100000 6 10 107812 105568 6 5 3 6 4 6 4 2 5 1 5 6 4 5 1 3 1 2 2 5 124065 140875 29890 80077 116532 35394 9 10 82107 88302 1 2 2 3 5 3 5 1 1 4 9 6 3 5 8 2 5 6 7 5 22670 3735 33660 92823 139960 89319 83335 158330 117349 6 10 181257 173221 5 3 3 4 3 1 5 1 2 1 3 6 3 1 6 2 3 6 4 3 76902 46253 123092 2661...
output:
-1 3 -1 1 2 1 1 -1 -1 2 1 1 2 3 2 2 -1 1 -1 2 4 2 -1 2 2 1 1 2 1 -1 1 2 2 1 -1 -1 2 3 3 -1 1 1 1 2 2 3 -1 -1 -1 -1 2 2 1 2 1 1 -1 2 3 -1 2 -1 1 1 1 2 -1 3 -1 -1 1 1 3 1 3 1 2 3 1 -1 3 2 1 1 1 -1 2 -1 1 -1 1 1 2 2 3 3 2 2 -1 -1 2 -1 1 2 -1 -1 2 3 2 -1 -1 1 2 2 -1 3 1 2 2 1 1 3 -1 4 1 -1 1 1 -1 1 -1 4...
result:
wrong answer 2nd numbers differ - expected: '-1', found: '3'