QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390108 | #2647. Environment-Friendly Travel | kevinyang# | AC ✓ | 312ms | 11696kb | C++20 | 9.4kb | 2024-04-15 04:23:38 | 2024-04-15 04:23:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
/* Macros {{{ */
/* A lot of this is from some of Benq's submissions
[https://codeforces.com/profile/Benq]
Ugly af to the eyes, but with vim fold its barable
Hopefully c++20 concepts can make all this stuff must cleaner */
/* Basics {{{ */
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define mp make_pair
#define fi first
#define se second
#define arr array
#define ve vector
using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpi = vector<pi>;
using vpll = vector<pll>;
using vpld = vector<pld>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvld = vector<vld>;
using vvpi = vector<vpi>;
using vvpll = vector<vpll>;
using vvpld = vector<vpld>;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz size()
#define rsz(a) resize(a)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define For(i, a, b) for (int i = a; i < b; ++i)
#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(a) For(_, 0, a)
#define each(a, x) for (auto &a : x)
#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)
template <typename T, typename U>
inline void cmin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void cmax(T &x, U y) {
if (x < y) x = y;
}
/*}}}*/
/* IO {{{ */
/* Template Macros {{{ */
#define tcT template <class T
#define tcTU tcT, class U
#define tcTUU tcT, class... U
/*}}}*/
inline namespace Helpers { /*{{{*/
tcT, class = void > struct is_iterable : false_type {};
tcT > struct is_iterable<
T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>
: true_type {};
tcT > constexpr bool is_iterable_v = is_iterable<T>::value;
tcT, class = void > struct is_readable : false_type {};
tcT > struct is_readable<T, typename std::enable_if_t<is_same_v<
decltype(cin >> declval<T &>()), istream &>>>
: true_type {};
tcT > constexpr bool is_readable_v = is_readable<T>::value;
tcT, class = void > struct is_printable : false_type {};
tcT > struct is_printable<T, typename std::enable_if_t<is_same_v<
decltype(cout << declval<T>()), ostream &>>>
: true_type {};
tcT > constexpr bool is_printable_v = is_printable<T>::value;
} /* namespace Helpers */
/*}}}*/
inline namespace Input { /*{{{*/
tcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
tcTUU > void re(T &t, U &...u);
tcTU > void re(pair<T, U> &p); /* pairs */
/* re: read{{{ */
tcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {
cin >> x;
} /* default */
tcT > typename enable_if<needs_input_v<T>, void>::type re(
T &i); // vectors, arrays, etc...
tcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs
tcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {
each(x, i) re(x);
}
tcTUU > void re(T &t, U &...u) {
re(t);
re(u...);
} /* read multiple}}} */
/* rv: resize and read vectors{{{ */
void rv(size_t) {}
tcTUU > void rv(size_t N, ve<T> &t, U &...u);
template <class... U>
void rv(size_t, size_t N2, U &...u);
tcTUU > void rv(size_t N, ve<T> &t, U &...u) {
t.rsz(N);
re(t);
rv(N, u...);
}
template <class... U>
void rv(size_t, size_t N2, U &...u) {
rv(N2, u...);
} /*}}}*/
/* dumb shortcuts to read in ints{{{ */
void decrement() {} /* subtract one from each */
tcTUU > void decrement(T &t, U &...u) {
--t;
decrement(u...);
}
#define ints(...) \
int __VA_ARGS__; \
re(__VA_ARGS__);
#define int1(...) \
ints(__VA_ARGS__); \
decrement(__VA_ARGS__); /*}}}*/
} /* namespace Input */
/*}}}*/
inline namespace ToString { /*{{{*/
tcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;
/* ts: string representation to print */
tcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {
stringstream ss;
ss << fixed << setprecision(15) << v;
return ss.str();
} /* default */
tcT > str bit_vec(T t) { /* bit vector to string */
str res = "{";
For(i, 0, t.sz) res += ts(t[i]);
res += "}";
return res;
}
str ts(ve<bool> v) { return bit_vec(v); }
template <size_t SZ>
str ts(bitset<SZ> b) {
return bit_vec(b);
} /* bit vector */
tcTU > str ts(pair<T, U> p); /* pairs */
tcT > typename enable_if<needs_output_v<T>, str>::type ts(
T v); /* vectors, arrays */
tcTU > str ts(pair<T, U> p) { return "(" + ts(p.fi) + ", " + ts(p.se) + ")"; }
tcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {
/* convert container to string w/ separator sep */
bool fst = 1;
str res = "";
for (const auto &x : v) {
if (!fst) res += sep;
fst = 0;
res += ts(x);
}
return res;
}
tcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {
return "{" + ts_sep(v, ", ") + "}";
}
/* for nested DS */
template <int, class T>
typename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
return {ts(v)};
}
template <int lev, class T>
typename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
if (lev == 0 || !v.sz) return {ts(v)};
ve<str> res;
for (const auto &t : v) {
if (res.sz) res.back() += ",";
ve<str> tmp = ts_lev<lev - 1>(t);
res.insert(end(res), all(tmp));
}
For(i, 0, res.sz) {
str bef = " ";
if (i == 0) bef = "{";
res[i] = bef + res[i];
}
res.back() += "}";
return res;
}
} /* namespace ToString */
/*}}}*/
inline namespace Output { /*{{{*/
template <class T>
void pr_sep(ostream &os, str, const T &t) {
os << ts(t);
}
template <class T, class... U>
void pr_sep(ostream &os, str sep, const T &t, const U &...u) {
pr_sep(os, sep, t);
os << sep;
pr_sep(os, sep, u...);
}
/* print w/ no spaces */
template <class... T>
void pr(const T &...t) {
pr_sep(cout, "", t...);
}
/* print w/ spaces, end with newline */
void ps() { cout << "\n"; }
template <class... T>
void ps(const T &...t) {
pr_sep(cout, " ", t...);
ps();
}
/* debug to cerr */
template <class... T>
void dbg_out(const T &...t) {
pr_sep(cerr, " | ", t...);
cerr << endl;
}
void loc_info(int line, str names) {
cerr << "Line(" << line << ") -> [" << names << "]: ";
}
template <int lev, class T>
void dbgl_out(const T &t) {
cerr << "\n\n" << ts_sep(ts_lev<lev>(t), "\n") << "\n" << endl;
}
} /* namespace Output */
/*}}}}}}}}}*/
int xs, ys;
int xd, yd;
int b;
int c0;
int T;
vi cis;
int n;
vpi stations;
vvpi edges;
vpi real_stations;
vvi dst;
vvi weight;
vvi dp;
map<pi, int> node_index;
void solve() {
re(xs, ys);
re(xd, yd);
re(b);
re(c0);
re(T), rv(T, cis);
re(n);
int ci = 0;
node_index[{ xs, ys }] = ci++;
vpi cur_nodes;
edges.rsz(n);
for(int i=0; i<n; ++i) {
pi tmp; re(tmp); cur_nodes.pb(tmp);
if(!node_index.count(tmp)) node_index[tmp] = ci++;
int tmp2; re(tmp2);
rv(tmp2, edges[i]);
}
if(!node_index.count({ xd, yd })) node_index[{ xd, yd }] = ci++;
int si = node_index[{ xs, ys }];
int di = node_index[{ xd, yd }];
real_stations.rsz(node_index.size());
weight.assign(node_index.size(), vi(node_index.size(), 100000));
for(int idx=0; idx<node_index.size(); ++idx) {
weight[si][idx] = c0, weight[idx][di] = c0, weight[idx][idx] = 0;
}
for(auto [u, idx] : node_index) real_stations[idx] = u;
for(int i=0; i<cur_nodes.sz; ++i) {
int idx1 = node_index[cur_nodes[i]];
for(auto [j, l] : edges[i]) {
int idx2 = node_index[cur_nodes[j]];
cmin(weight[idx1][idx2], cis[l-1]);
cmin(weight[idx2][idx1], cis[l-1]);
}
}
dst.assign(node_index.size(), vi(node_index.size(), 0));
for(int i=0; i<node_index.size(); ++i) for(int j=i+1; j<node_index.size(); ++j) {
auto [a0, a1] = real_stations[i];
auto [b0, b1] = real_stations[j];
int distsq = (a0-b0)*(a0-b0) + (a1-b1)*(a1-b1);
int l = 1, r = distsq;
while(l < r) {
int m = (l+r)>>1;
if(m*m < distsq) l = m+1;
else r = m;
}
dst[i][j] = l, dst[j][i] = l;
}
dp.assign(node_index.sz, vi(b+1, INT_MAX/2));
dp[di].assign(b+1, 0);
for(int cd=b-1; cd>=0; --cd) for(int i=0; i<node_index.sz; ++i) {
for(int j=0; j<node_index.sz; ++j) {
if(i == j) continue;
int dist = dst[i][j];
int cst = dist * weight[i][j];
if(cd+dist <= b) cmin(dp[i][cd], cst + dp[j][cd+dist]);
}
}
int res = dp[si][0];
ps(res < INT_MAX/2 ? res : -1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/* cout << fixed << setprecision(6); */
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) solve();
return 0;
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3884kb
input:
1 1 10 2 12 100 2 10 50 3 2 3 2 1 1 2 2 5 5 1 2 1 9 3 0
output:
850
result:
ok single line: '850'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
1 1 9 8 10 100 1 1 2 3 5 1 1 1 7 3 1 0 1
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
3 5 6 3 4 100 2 80 70 4 3 1 3 1 1 3 1 2 1 6 7 2 2 2 3 2 8 2 2 1 2 3 2 11 7 0
output:
400
result:
ok single line: '400'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
1 3 1 3 4 100 2 80 70 4 3 1 3 1 1 3 1 2 1 6 7 2 2 2 3 2 8 2 2 1 2 3 2 11 7 0
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
10 2 2 2 12 100 2 10 40 4 2 4 2 1 1 3 2 4 6 1 2 1 9 6 1 3 1 10 4 0
output:
720
result:
ok single line: '720'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
2 2 11 2 20 100 3 50 1 80 2 2 3 2 1 1 1 3 11 3 1 0 2
output:
209
result:
ok single line: '209'
Test #7:
score: 0
Accepted
time: 131ms
memory: 11604kb
input:
17 76 71 92 45 100 100 40 11 29 45 1 34 37 12 37 27 37 19 35 46 14 19 15 10 29 2 31 41 2 43 30 35 21 8 38 27 13 44 22 24 23 22 32 4 12 40 34 33 25 29 48 34 40 31 23 26 46 44 31 17 8 25 13 8 7 22 29 12 39 46 40 34 26 16 26 9 28 31 11 8 25 22 26 39 19 16 6 21 44 24 3 25 29 2 45 39 20 1 24 46 2 36 17 4...
output:
-1
result:
ok single line: '-1'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3588kb
input:
0 0 0 12 15 100 2 1 10 8 0 2 2 2 1 1 2 0 4 1 3 2 8 4 1 3 1 0 6 1 4 2 0 8 2 5 1 6 2 3 10 1 7 1 0 10 1 7 2 0 12 0
output:
300
result:
ok single line: '300'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
0 0 0 12 27 100 2 1 10 8 0 2 2 2 1 1 2 0 4 1 3 2 8 4 1 3 1 0 6 1 4 2 0 8 2 5 1 6 2 3 10 1 7 1 0 10 1 7 2 0 12 0
output:
268
result:
ok single line: '268'
Test #10:
score: 0
Accepted
time: 312ms
memory: 11696kb
input:
56 66 48 89 100 100 100 15 44 28 16 25 37 32 44 43 16 48 3 12 41 32 40 8 45 1 41 3 14 29 43 47 36 32 48 46 18 2 10 18 24 17 35 15 13 30 22 11 17 30 32 4 14 4 14 40 33 41 43 34 48 38 40 20 22 14 3 23 23 24 44 43 11 18 27 11 40 15 37 42 13 7 29 26 36 11 13 32 29 38 11 38 48 4 7 13 1 38 36 14 19 39 12 ...
output:
426
result:
ok single line: '426'
Test #11:
score: 0
Accepted
time: 3ms
memory: 3868kb
input:
0 0 56 52 100 100 10 1 2 3 4 5 6 7 8 9 10 1000 0 0 10 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 1 9 0 2 9 0 3 9 0 4 9 0 5 9 0 6 9 0 7 9 0 8 9 0 9 9 0 10 9 19 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 11 1 12 2 13 3 14 4 15 5 16 6 17 7 18 8 19 9 20 10 9 17 0 8 17 0 7 17 0 6 17 0 5 17 0 4 17 0 3 17 0 2 17 0...
output:
4656
result:
ok single line: '4656'