QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#364729 | #5073. Elden Ring | Baiyu0123 | WA | 0ms | 19992kb | C++14 | 2.7kb | 2024-03-24 16:16:52 | 2024-03-24 16:16:53 |
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 (u!=1&&a[1]-1ll*dis[u]*x<=a[u]) {
dis[u]=lim;
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&&g[v]!=-1) {
dis[v]=dis[u]+1;
if (dis[v]>g[v]) 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&&g[v]!=-1) {
dis[v]=dis[u]+1;
if (dis[v]>g[v]) 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
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 19992kb
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:
-1 4
result:
wrong answer 1st numbers differ - expected: '2', found: '-1'