QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#628129 | #6306. Chase Game | TokaiZaopen | WA | 1ms | 5688kb | C++20 | 3.3kb | 2024-10-10 18:46:27 | 2024-10-10 18:46:27 |
Judging History
answer
#include <bits/stdc++.h>
#define int ll
using ll = long long;
// #define MING_LE
const int mod = 998244353;
const int inf = 1e18;
using namespace std;
struct info
{
int to;
int w;
};
struct node
{
int dis;
int u;
bool operator>(const node &o) const
{
return dis > o.dis;
}
};
int n, m;
int pos, d;
vector<vector<info>> a;
vector<vector<info>> b;
int dis[100010];
int w[100010];
int sum[100010];
bool vis[100010];
bool vlid[100010];
bool in[100010];
void dij(vector<vector<info>> &e, int st);
void solve();
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int __ = 1;
// cin >> __;
while (__--)
solve();
return 0;
}
void solve()
{
cin >> n >> m >> pos >> d;
a.resize(n + 5);
b.resize(n + 5);
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
a[u].push_back({v, 1});
a[v].push_back({u, 1});
}
dij(a, pos);
for (int i = 1; i <= n; i++)
{
if (dis[i] == d - 1)
vlid[i] = 1;
if (dis[i] < d)
in[i] = 1;
for (auto it : a[i])
{
int to = it.to;
if (dis[to] >= d)
continue;
b[i].push_back({to, d - dis[to]});
}
}
dij(a, n);
sum[0] = 0;
int now = d;
for (int i = 1; i <= n; i++)
{
now--;
if (now <= 0)
now = d;
sum[i] = sum[i - 1] + now;
}
for (int i = 1; i <= n; i++)
{
if (dis[i] < inf)
w[i] = sum[dis[i]];
else
w[i] = inf;
}
int ans = 0;
now = d;
for (int i = 1; i <= dis[1]; i++)
{
ans += now;
now--;
if (now <= 0)
now = d;
}
dij(b, 1);
ans = min(w[1], ans);
for (int i = 1; i <= n; i++)
{
if (vlid[i])
{
for (auto it : a[i])
if (!in[it.to])
ans = min(ans, dis[i] + w[it.to] + d);
}
#ifdef MING_LE
cout << i << " " << dis[i] << " " << w[i] << '\n';
#endif
}
cout << ans << '\n';
}
void dij(vector<vector<info>> &e, int st)
{
for (int i = 1; i <= n; i++)
{
vis[i] = 0;
dis[i] = inf;
}
dis[st] = 0;
priority_queue<node, vector<node>, greater<node>> q;
q.push({dis[st], st});
while (!q.empty())
{
int x = q.top().u;
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (auto it : e[x])
{
int u = it.to, w = it.w;
if (dis[u] > dis[x] + w)
{
dis[u] = dis[x] + w;
q.push({dis[u], u});
}
}
}
}
/*
5 5 3 1
1 2
2 4
4 5
1 3
3 5
*/
/*
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
*/
/*
5 4 3 3
1 2
2 3
3 4
4 5
*/
/*
6 6 4 1
1 3
3 6
1 2
2 4
4 5
5 6
*/
/*
6 7 2 3
1 2
1 3
2 4
3 4
4 6
2 5
5 6
*/
/*
20 26 4 4
1 2
2 3
3 4
4 5
5 6
6 19
4 8
4 9
4 10
4 11
4 12
4 13
1 14
14 8
14 15
15 9
15 16
16 10
16 17
17 11
17 18
18 12
18 7
7 13
19 7
19 20
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3876kb
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: 3644kb
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: 5668kb
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: 1ms
memory: 5688kb
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: 3644kb
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: 3516kb
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: 0
Accepted
time: 0ms
memory: 3524kb
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:
24
result:
ok 1 number(s): "24"
Test #8:
score: -100
Wrong Answer
time: 0ms
memory: 3572kb
input:
10 20 6 10 5 4 1 7 6 8 1 2 6 1 5 2 5 1 2 4 5 3 3 2 10 3 9 10 7 9 3 1 5 9 1 8 8 3 8 7 6 4 2 7
output:
17
result:
wrong answer 1st numbers differ - expected: '15', found: '17'