QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#179075 | #5081. Forbidden Turns | shimeming | WA | 0ms | 3836kb | C++14 | 2.4kb | 2023-09-14 17:35:16 | 2023-09-14 17:35:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define debug(x) { cout << #x << ' ' << x << '\n'; }
#define pb push_back
#define mp make_pair
#define mt make_tuple
template<typename T> using Min_heap = priority_queue<T, vector<T>, greater<T>>;
int m, n, k;
int v, w;
const int INF = INT_MAX;
vector<vector<pii>> G, rG;
vector<vector<int>> dis;
vector<vector<bool>> vis;
set<tuple<int, int, int>> forbid;
int dijkstra() {
Min_heap<tuple<int, int, int>> pq; // <W, ind_from, to>
for (pii i : G[v]) {
int nfrom = find(rG[i.Y].begin(), rG[i.Y].end(), mp(i.X, v)) - rG[i.Y].begin();
dis[i.Y][nfrom] = i.X;
pq.push({dis[i.Y][nfrom], nfrom, i.Y});
}
// cerr << (forbid.find(mt(4, 1, 5))!=forbid.end()) << ' ' << (forbid.find(mt(1, 5, 4))!=forbid.end()) << '\n';
while (!pq.empty()) {
auto [D, ind_from, now] = pq.top();
pq.pop();
// if (D > rG[now][ind_from].X) continue;
if (vis[now][ind_from]) continue;
vis[now][ind_from] = 1;
int from = rG[now][ind_from].Y;
for (pii i : G[now]) {
if (forbid.find(mt(from, now, i.Y)) != forbid.end()) continue;
int nfrom = find(rG[i.Y].begin(), rG[i.Y].end(), mp(i.X, now)) - rG[i.Y].begin();
if (dis[i.Y][nfrom] > D + i.X) {
dis[i.Y][nfrom] = D + i.X;
pq.push({dis[i.Y][nfrom], nfrom, i.Y});
}
}
}
int ans = INF;
for (int i = 0; i < (int)rG[w].size(); i++) {
ans = min(ans, dis[w][i]);
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n >> k;
cin >> v >> w;
G.resize(n);
rG.resize(n);
dis.resize(n);
vis.resize(n);
for (int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c;
G[x].pb({c, y});
rG[y].pb({c, x});
// if (x == v) dis[y].pb(c);
// else dis[y].pb(INF);
dis[y].pb(INF);
vis[y].pb(false);
}
for (int i = 0; i < k; i++) {
int x, y, z;
cin >> x >> y >> z;
forbid.insert({x, y, z});
}
int ans = dijkstra();
/*
for (int i = 0; i < n; i++) {
cerr << i << ": \n";
for (int j = 0; j < (int)rG[i].size(); j++) {
cerr << rG[i][j].Y << "-" << rG[i][j].X << " " << dis[i][j] << '\n';
}
cerr << '\n';
}
*/
if (ans == INF) cout << "-1\n";
else cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
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: 3616kb
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: 3800kb
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: 0ms
memory: 3612kb
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: 0ms
memory: 3636kb
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: 0ms
memory: 3628kb
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: 0ms
memory: 3612kb
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: 3836kb
input:
0 1 0 0 0
output:
-1
result:
wrong answer 1st lines differ - expected: '0', found: '-1'