QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#304118 | #5081. Forbidden Turns | warner1129# | WA | 1ms | 3560kb | C++20 | 3.2kb | 2024-01-13 14:33:16 | 2024-01-13 14:33:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<ranges::range T, class = enable_if_t<!is_convertible_v<T, string_view>>>
istream& operator>>(istream &s, T &&v) { for (auto &&x : v) s >> x; return s; }
template<ranges::range T, class = enable_if_t<!is_convertible_v<T, string_view>>>
ostream& operator<<(ostream &s, T &&v) { for (auto &&x : v) s << x << ' '; return s; }
#ifdef LOCAL
template<class... T> void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); }
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#define debug(...) ((void)0)
#endif
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using Pt = pair<double, double>;
template<class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
constexpr int mod = 1e9 + 7, inv2 = (mod + 1) / 2;
constexpr double eps = 1e-9;
template<class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template<class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }
void solve() {
int m, n, k;
cin >> m >> n >> k;
int S, T;
cin >> S >> T;
vector<array<int, 3>> edg(m);
for (auto &[u, v, w] : edg) {
cin >> u >> v >> w;
}
sort(all(edg));
auto getid = [&](int a, int b) -> int {
auto it = lower_bound(all(edg), array<int, 3>{a, b, -1});
if (it == edg.end()) return -1;
if ((*it)[0] != a or (*it)[1] != b) return -1;
return it - edg.begin();
};
vector<vector<int>> fob(m);
for (int i = 0; i < k; i++) {
int a, b, c;
cin >> a >> b >> c;
int x = getid(a, b);
int y = getid(b, c);
if (x != -1 and y != -1) {
fob[x].push_back(y);
}
}
for (int i = 0; i < m; i++) {
sort(all(fob[i]));
}
vector<vector<int>> out(n), in(n);
for (int i = 0; i < m; i++) {
out[edg[i][0]].push_back(i);
in[edg[i][1]].push_back(i);
}
vector G(m, vector<pair<int, i64>>{});
for (int i = 0; i < n; i++) {
sort(all(in[i]));
sort(all(out[i]));
for (int x : in[i]) {
for (int y : out[i]) {
if (!binary_search(all(fob[x]), y)) {
G[x].emplace_back(y, edg[y][2]);
}
}
}
}
vector<i64> dis(m, inf<i64>);
priority_queue<pair<i64, int>> pq;
for (int x : out[S]) {
chmin<i64>(dis[x], edg[x][2]);
pq.emplace(-dis[x], x);
}
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
d = -d;
if (d != dis[u]) continue;
for (auto [v, w] : G[u])
if (chmin(dis[v], w + d)) {
pq.emplace(-dis[v], v);
}
}
i64 ans = inf<i64>;
for (int x : in[T])
chmin(ans, dis[x]);
if (ans == inf<i64>) cout << -1 << '\n';
else cout << ans << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
input:
9 7 3 3 2 6 3 2 3 0 3 0 1 12 1 0 4 1 2 2 1 5 4 4 1 8 5 4 7 5 2 5 0 1 2 4 1 5 1 5 2
output:
36
result:
ok single line: '36'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
4 4 1 0 3 0 1 2 1 2 3 0 2 7 2 3 10 0 1 2
output:
17
result:
ok single line: '17'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
4 4 0 0 3 0 1 2 1 2 3 0 2 7 2 3 10
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
4 4 1 1 0 1 2 3 2 3 10 3 2 12 2 0 7 1 2 0
output:
32
result:
ok single line: '32'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
13 8 5 0 5 0 2 3 2 1 7 4 2 10 2 3 10 3 2 12 2 5 10 3 6 10 6 7 5 7 3 5 6 4 10 4 1 0 1 4 10 4 6 10 0 2 1 0 2 5 3 2 5 2 3 2 6 4 2
output:
63
result:
ok single line: '63'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
4 4 2 1 0 1 2 3 2 3 10 3 2 12 2 0 7 1 2 0 2 3 2
output:
-1
result:
ok single line: '-1'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
4 4 0 1 0 1 2 3 2 3 2 3 2 3 2 0 7
output:
10
result:
ok single line: '10'
Test #8:
score: -100
Wrong Answer
time: 0ms
memory: 3488kb
input:
0 1 0 0 0
output:
-1
result:
wrong answer 1st lines differ - expected: '0', found: '-1'