QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#628240 | #6306. Chase Game | TokaiZaopen | WA | 0ms | 3864kb | C++23 | 3.8kb | 2024-10-10 19:15:36 | 2024-10-10 19:15:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct dijkstra
{
int n;
vector<vector<pair<int, int>>> &G;
vector<int> distance;
void build(int &s)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.emplace(0, s);
while (!q.empty())
{
auto [d, u] = q.top();
q.pop();
if (d > distance[u])
{
continue;
}
for (auto &it : G[u])
{
auto [v, w] = it;
if (distance[v] > d + w)
{
distance[v] = d + w;
q.emplace(distance[v], v);
}
}
}
}
public:
dijkstra(int start, vector<vector<pair<int, int>>> &a) : G(a), n(a.size())
{
distance = vector<int>(n + 1, LONG_LONG_MAX / 4);
distance[start] = 0;
build(start);
};
int dis(int end)
{
return distance[end];
}
};
struct graphy
{
vector<vector<pair<int, int>>> g;
int n;
graphy(int n) : n(n)
{
g.resize(n + 1);
}
void add(int u, int v, int w)
{
g[u].emplace_back(v, w);
}
};
int solve()
{
int n, m, pos, d;
cin >> n >> m >> pos >> d;
graphy g(n);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
g.add(u, v, 1);
g.add(v, u, 1);
}
vector<int> sum(n + 1);
int now = d - 1;
sum[0] = d;
if (now <= 0)
{
now = d;
}
for (int i = 1; i <= n; i++)
{
sum[i] = sum[i - 1] + now--;
if (now <= 0)
{
now = d;
}
}
dijkstra d1(n, g.g);
dijkstra d2(pos, g.g);
queue<int> q;
vector<bool> vis(n + 1);
graphy g2(n);
vis[pos] = true;
q.emplace(pos);
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto &it : g.g[u])
{
auto [v, w] = it;
if (!vis[v] && d2.dis(v) < d)
{
vis[v] = true;
q.emplace(v);
}
}
}
for (int i = 1; i <= n; i++)
{
if (vis[i])
{
for (auto &it : g.g[i])
{
auto [v, w] = it;
if (vis[v])
{
g2.add(i, v, d - d2.dis(v));
g2.add(v, i, d - d2.dis(i));
}
}
}
}
if (!vis[1])
{
for (auto it : g.g[1])
{
auto [v, w] = it;
if (vis[v])
{
g2.add(1, v, d - d2.dis(v));
}
}
}
dijkstra d3(1, g2.g);
while (!q.empty())
{
q.pop();
}
q.emplace(pos);
int ans = LONG_LONG_MAX / 2;
vis = vector<bool>(n + 1);
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto &it : g.g[u])
{
auto [v, w] = it;
if (!vis[v] && d2.dis(v) < d)
{
vis[v] = true;
q.emplace(v);
}
if (d2.dis(v) == d)
{
ans = min(ans, sum[d1.dis(v)] + d3.dis(u));
}
}
}
for (auto it : g.g[1])
{
if (!vis[it.first])
{
ans = min(ans, sum[d1.dis(it.first)]);
}
}
cout << ans << endl;
return 0;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
while (test--)
{
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3560kb
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: 3844kb
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: 3780kb
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: 3560kb
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: 3612kb
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: 3864kb
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:
4611686018427387903
result:
wrong answer 1st numbers differ - expected: '24', found: '4611686018427387903'