QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#357199 | #7404. Back and Forth | ckiseki | AC ✓ | 16ms | 4320kb | C++23 | 4.9kb | 2024-03-18 19:17:29 | 2024-03-18 19:17:29 |
Judging History
answer
// An AC a day keeps the doctor away.
#include <bits/stdc++.h>
using namespace std;
/*{{{*/
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
template <typename T, T MOD> class Modular {
public:
constexpr Modular() : v() {}
template <typename U> Modular(const U &u) { v = (0 <= u && u < MOD ? u : (u%MOD+MOD)%MOD); }
template <typename U> explicit operator U() const { return U(v); }
T operator()() const { return v; }
#define REFOP(type, expr...) Modular &operator type (const Modular &rhs) { return expr, *this; }
REFOP(+=, v += rhs.v - MOD, v += MOD & (v >> width)) ; REFOP(-=, v -= rhs.v, v += MOD & (v >> width))
// fits for MOD^2 <= 9e18
REFOP(*=, v = static_cast<T>(1LL * v * rhs.v % MOD)) ; REFOP(/=, *this *= inverse(rhs.v))
#define VALOP(op) friend Modular operator op (Modular a, const Modular &b) { return a op##= b; }
VALOP(+) ; VALOP(-) ; VALOP(*) ; VALOP(/)
Modular operator-() const { return 0 - *this; }
friend bool operator == (const Modular &lhs, const Modular &rhs) { return lhs.v == rhs.v; }
friend bool operator != (const Modular &lhs, const Modular &rhs) { return lhs.v != rhs.v; }
friend std::istream & operator>>(std::istream &I, Modular &m) { T x; I >> x, m = x; return I; }
friend std::ostream & operator<<(std::ostream &O, const Modular &m) { return O << m.v; }
private:
constexpr static int width = sizeof(T) * 8 - 1;
T v;
static T inverse(T a) {
// copy from tourist's template
T u = 0, v = 1, m = MOD;
while (a != 0) {
T t = m / a;
m -= t * a; std::swap(a, m);
u -= t * v; std::swap(u, v);
}
assert(m == 1);
return u;
}
};
using lld = int64_t;
using llf = long double;
template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >;
template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >;
template <typename V, typename T> int get_pos(const V &v, T x) { return lower_bound(all(v),x) - begin(v); }
template <typename V> void sort_uni(V &v) { sort(all(v)), v.erase(unique(all(v)),end(v)); }
template <typename T> bool chmin(T &x, const T &v) { return v < x ? (x=v, true) : false; }
template <typename T> bool chmax(T &x, const T &v) { return x < v ? (x=v, true) : false; }
constexpr inline lld cdiv(lld x, lld m) { return x/m + (x%m ? (x<0) ^ (m>0) : 0); } // ceiling divide
constexpr inline lld modpow(lld e,lld p,lld m) { lld r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }/*}}}*/
constexpr llf eps = 1e-9;
constexpr lld maxn = 3025, INF = 1e18, mod = 998244353, KK = 14699, inf = 1e9;
using Mint = Modular<int, mod>;
Mint modpow(Mint e, uint64_t p) { Mint r = 1; while (p) (p&1) && (r *= e), e *= e, p >>= 1; return r; } // 0^0 = 1
const auto dummy = [] { return cin.tie(nullptr)->sync_with_stdio(false); }();
void solve() {
int n, m, s, t;
cin >> n >> m >> s >> t;
--s, --t;
vector<int> cost(n);
for (int &x : cost) cin >> x;
vector<vector<int>> g(n), rg(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].emplace_back(v);
rg[v].emplace_back(u);
}
vector<vector<int>> d(n, vector<int>(n, inf));
for (int S = 0; S < n; S++) {
min_heap<pair<int,int>> h;
h.emplace(d[S][S] = 0, S);
while (!h.empty()) {
auto [D, i] = h.top(); h.pop();
if (d[S][i] != D) continue;
for (int j : g[i]) {
if (d[S][j] > d[S][i] + cost[j]) {
d[S][j] = d[S][i] + cost[j];
h.emplace(d[S][j], j);
}
}
}
}
min_heap<tuple<int,int,int>> pq;
vector<vector<int>> dp(n, vector<int>(n, inf));
auto relax = [&](int u, int v, int w) {
if (dp[u][v] > w) {
dp[u][v] = w;
pq.emplace(dp[u][v], u, v);
}
};
pq.emplace(dp[s][s] = cost[s], s, s);
while (!pq.empty()) {
auto [D, a, b] = pq.top(); pq.pop();
if (D != dp[a][b]) continue;
for (int x : g[a])
relax(x, b, dp[a][b] + (x==b ? 0 : cost[x]));
for (int x : rg[b])
relax(a, x, dp[a][b] + (x==a ? 0 : cost[x]));
if (a != b) {
relax(b, a, dp[a][b] + d[a][b] - cost[b]);
}
}
if (dp[t][t] == inf)
cout << -1 << '\n';
else
cout << dp[t][t] << '\n';
}
signed main() {
int T;
cin >> T;
while (T--)
solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3764kb
input:
3 4 5 1 4 1 1 1 1 1 2 2 3 3 1 4 2 3 4 4 4 1 2 1 1 1 1 1 2 2 3 3 4 4 1 4 8 1 3 1 100 1 1 1 2 2 1 2 3 3 2 1 4 4 1 3 4 4 3
output:
4 4 3
result:
ok 3 number(s): "4 4 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
1 2 0 1 2 1 1
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
10 20 40 5 16 2 1 1 1 2 1 1 2 1 1 1 2 1 1 1 2 1 2 1 2 7 2 8 5 15 4 17 13 11 6 9 2 12 13 12 1 1 8 2 20 6 9 18 3 2 15 10 12 4 17 11 5 19 15 15 9 14 7 11 2 1 15 16 1 13 3 11 16 5 10 19 6 1 3 13 14 20 11 6 19 7 9 20 12 17 18 13 9 4 18 3 19 13 17 19 9 19 10 8 6 20 40 10 1 2 2 2 2 1 2 1 2 2 2 2 2 1 2 2 1 ...
output:
15 14 11 12 15 8 9 13 12 12
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 3852kb
input:
10 20 40 17 11 8 3 9 8 9 2 9 3 8 10 4 7 5 2 5 2 7 5 8 2 14 1 6 5 9 13 13 20 15 7 16 12 9 16 9 11 1 2 8 13 3 17 18 2 2 15 4 15 5 18 11 18 15 1 18 11 8 9 20 1 7 4 4 14 1 8 5 19 13 12 5 3 12 8 7 14 3 18 2 19 11 13 12 6 20 10 20 6 19 7 10 20 17 16 10 19 19 3 15 14 20 40 20 2 6 2 3 1 8 3 9 8 5 2 7 8 2 5 ...
output:
50 32 30 52 42 45 34 55 40 55
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 1ms
memory: 3804kb
input:
10 20 40 18 6 66 8 72 32 88 52 22 28 50 62 1 73 83 81 14 82 50 90 90 87 13 10 11 18 16 12 20 8 11 15 3 12 15 2 14 13 17 7 2 5 18 3 4 2 15 4 2 15 7 9 2 1 17 14 14 18 15 8 16 1 4 17 14 19 5 8 20 11 7 5 19 16 6 10 1 4 14 20 5 6 12 13 8 13 2 18 10 17 9 20 12 15 1 5 7 6 16 18 9 3 20 40 10 20 46 66 84 50 ...
output:
525 239 429 400 328 517 229 391 664 376
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 3ms
memory: 3640kb
input:
4 50 100 16 46 2 3 3 1 2 1 2 5 2 5 5 5 5 1 3 1 1 5 2 1 3 5 5 3 3 1 4 5 1 3 1 5 3 4 1 2 1 4 4 1 1 3 5 1 2 5 3 3 3 3 28 1 7 18 3 17 48 5 27 42 18 49 6 5 26 27 48 25 15 21 41 15 32 31 39 30 30 22 23 2 35 9 19 37 44 22 50 43 14 46 26 3 24 2 37 23 25 30 39 19 36 41 45 17 31 50 26 38 14 38 30 3 3 10 13 7 ...
output:
36 38 23 36
result:
ok 4 number(s): "36 38 23 36"
Test #7:
score: 0
Accepted
time: 11ms
memory: 4088kb
input:
1 200 300 169 170 2 1 2 1 1 2 1 1 2 2 2 2 2 1 2 2 2 1 1 2 2 2 2 2 2 1 2 1 2 1 2 2 2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 1 2 2 2 1 2 2 2 1 2 1 1 1 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 1 2 2 1 2 1 1 1 1 2 2 1 2 2 2 1 1 1 2 1 2 1 1 1 1 1 1 1 2 2 1 2 2 1 1 2 1 2 2 2 1 1 1 2 1 2 1 ...
output:
30
result:
ok 1 number(s): "30"
Test #8:
score: 0
Accepted
time: 11ms
memory: 4320kb
input:
1 200 300 100 39 11 5 9 6 5 1 7 4 3 3 13 4 5 12 8 9 2 1 5 4 6 7 10 12 12 4 13 1 7 14 1 8 15 10 3 3 11 15 11 8 4 5 9 15 7 13 12 13 1 14 9 15 5 1 7 2 3 5 6 7 12 10 9 8 12 11 15 15 8 10 9 12 14 1 1 10 3 15 10 7 2 2 10 10 9 13 15 10 7 3 8 1 15 12 3 14 13 15 11 10 3 1 8 4 9 13 5 1 8 15 10 11 15 6 5 1 10 ...
output:
264
result:
ok 1 number(s): "264"
Test #9:
score: 0
Accepted
time: 12ms
memory: 4320kb
input:
1 200 400 90 131 2 1 1 1 2 1 2 2 1 2 2 2 1 2 1 1 2 1 2 2 1 2 1 2 2 2 1 2 2 1 2 2 1 2 1 2 1 1 2 2 2 2 1 2 2 1 2 2 1 2 2 1 1 1 1 1 2 1 2 2 1 1 1 1 1 2 1 1 2 1 2 2 1 2 2 1 1 2 1 2 1 2 2 1 2 2 1 2 1 2 1 1 2 1 2 2 2 2 1 2 1 2 2 1 1 2 1 1 2 1 2 2 2 1 2 2 2 2 2 1 1 1 2 1 1 1 1 1 2 2 2 1 2 1 2 1 1 1 1 2 1 2...
output:
21
result:
ok 1 number(s): "21"
Test #10:
score: 0
Accepted
time: 11ms
memory: 4272kb
input:
1 200 400 196 119 15 11 14 6 9 12 12 1 13 2 5 9 15 10 11 12 15 13 9 10 15 1 15 2 2 10 15 3 5 3 15 13 13 1 1 13 10 10 11 1 12 7 4 3 8 7 13 13 5 13 1 3 1 1 1 4 10 10 6 13 9 7 1 13 13 7 7 10 14 7 9 2 3 8 1 2 15 1 5 11 14 15 12 5 15 11 12 14 10 14 14 14 15 7 5 1 10 7 14 12 9 5 7 9 8 10 2 5 5 15 14 9 4 3...
output:
102
result:
ok 1 number(s): "102"
Test #11:
score: 0
Accepted
time: 8ms
memory: 4316kb
input:
1 200 800 91 57 2 2 2 1 2 2 1 2 1 2 2 2 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 1 1 2 2 1 1 1 2 1 2 1 1 1 2 2 2 1 1 1 2 1 1 2 1 2 2 2 1 2 2 2 2 1 1 1 1 2 2 2 2 2 1 2 1 1 2 1 1 1 1 2 2 2 2 2 1 1 1 1 2 1 2 2 1 1 1 2 1 1 2 2 1 1 1 1 1 1 1 1 2 2 1 2 2 2 1 2 1 1 2 1 1 1 2 1 2 2 1 2 1 1 1 1 2 1 ...
output:
11
result:
ok 1 number(s): "11"
Test #12:
score: 0
Accepted
time: 12ms
memory: 4096kb
input:
1 200 800 26 27 7 5 1 8 14 11 5 5 15 15 3 2 6 2 7 7 4 2 4 5 9 1 13 15 11 2 13 10 10 12 3 4 2 6 1 1 10 11 7 2 8 13 13 10 14 4 14 4 11 9 11 6 14 10 4 14 12 10 10 2 8 6 10 12 13 13 12 15 14 10 15 4 9 8 9 1 15 4 11 15 9 9 12 5 4 3 10 1 2 10 10 13 1 6 8 8 3 5 13 6 3 4 5 11 1 1 5 5 14 3 1 10 12 9 3 7 14 3...
output:
55
result:
ok 1 number(s): "55"
Test #13:
score: 0
Accepted
time: 13ms
memory: 4144kb
input:
1 200 1600 91 175 2 1 2 1 2 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 2 2 2 1 1 1 1 2 2 1 2 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 1 2 2 2 1 2 2 1 2 1 2 2 1 2 2 2 1 1 2 1 1 1 2 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 1 1 1 1 1 1 2 2 2 1 1 1 1 2 2 2 1 2 2 1 1 1 2 1 1 1 2 1 1 2 1 2 2 1 1 ...
output:
8
result:
ok 1 number(s): "8"
Test #14:
score: 0
Accepted
time: 13ms
memory: 4124kb
input:
1 200 1600 73 167 1 4 6 11 1 13 4 5 7 4 12 4 1 8 10 10 4 2 1 15 1 6 7 13 14 6 11 11 13 15 9 5 14 12 1 14 3 15 2 11 4 3 5 11 8 15 6 2 1 5 8 15 3 7 1 5 6 14 11 13 9 2 3 14 5 3 3 2 5 14 5 7 6 8 4 10 8 13 3 9 7 3 9 7 7 12 4 12 7 9 5 5 1 13 10 9 4 3 3 2 12 12 12 7 4 5 2 11 12 9 11 3 7 14 9 8 4 11 11 7 7 ...
output:
37
result:
ok 1 number(s): "37"
Test #15:
score: 0
Accepted
time: 16ms
memory: 4124kb
input:
1 200 3200 36 187 3 8 13 7 8 3 2 9 12 8 15 5 8 4 5 13 12 15 3 2 6 8 9 12 14 7 10 15 7 13 6 10 6 2 12 9 8 2 13 13 3 13 3 13 8 8 11 3 7 12 6 6 14 8 11 5 5 6 10 12 3 1 8 6 14 6 11 12 4 10 6 4 4 7 8 9 1 11 11 9 9 5 4 10 4 12 5 12 15 3 15 4 6 14 14 8 13 15 7 11 1 6 10 5 2 1 5 2 1 5 8 2 12 8 13 12 11 13 1...
output:
22
result:
ok 1 number(s): "22"
Extra Test:
score: 0
Extra Test Passed