# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
544833 |
2022-04-02T19:29:32 Z |
Bungmint |
Robot (JOI21_ho_t4) |
C++17 |
|
857 ms |
81040 KB |
//Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
void dbg_out()
{
cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif
inline namespace RecursiveLambda{
template <typename Fun>
struct y_combinator_result{
Fun fun_;
template <typename T>
explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
template <typename...Args>
decltype(auto) operator()(Args &&...args){
return fun_(ref(*this), forward<Args>(args)...);
}
};
template <typename Fun>
decltype(auto) y_combinator(Fun &&fun){
return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}
};
void setIO(string s) // USACO
{
#ifndef LOCAL
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
struct Edge{
int to, c;
int p;
};
const int N = 1e5;
map<int, vector<Edge>> adj[N];
ll dp[N];
map<int, ll> psum[N], dp2[N];
void solve()
{
int n, m;
cin >> n >> m;
REP(m) {
int u, v, c, p;
cin >> u >> v >> c >> p;
u--, v--;
adj[u][c].pb({v, c, p});
adj[v][c].pb({u, c, p});
psum[u][c] += p, psum[v][c] += p;
}
memset(dp, 0x3f, sizeof(dp));
pqg<tuple<ll, int, int>> pq;
dp[0] = 0;
pq.push({0LL, 0, 0});
while (sz(pq)) {
auto [cost, u, c] = pq.top(); pq.pop();
if (c) {
if (cost != dp2[u][c]) continue;
for (auto &e : adj[u][c]) {
ll add = psum[u][c] - e.p;
if (ckmin(dp[e.to], cost + add)) {
pq.push({dp[e.to], e.to, 0});
}
}
}else{
if (cost != dp[u]) continue;
for (auto &v : adj[u]) {
for (auto &e : v.se) {
ll case1 = psum[u][e.c] - e.p;
if (ckmin(dp[e.to], case1 + cost)) {
pq.push({dp[e.to], e.to, 0});
}
ll case2 = e.p + cost;
if (ckmin(dp[e.to], case2)) {
pq.push({dp[e.to], e.to, 0});
}
if (!dp2[e.to].count(e.c) || ckmin(dp2[e.to][e.c], cost)) {
dp2[e.to][e.c] = cost;
pq.push({dp2[e.to][e.c], e.to, e.c});
}
}
}
}
}
cout << (dp[n - 1] > LINF ? -1 : dp[n - 1]);
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int testcase=1;
// cin >> testcase;
while (testcase--)
{
solve();
}
}
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
8 ms |
15080 KB |
Output is correct |
3 |
Correct |
8 ms |
15108 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
8 ms |
15188 KB |
Output is correct |
6 |
Correct |
7 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15324 KB |
Output is correct |
8 |
Correct |
8 ms |
15200 KB |
Output is correct |
9 |
Correct |
13 ms |
15752 KB |
Output is correct |
10 |
Correct |
10 ms |
15712 KB |
Output is correct |
11 |
Correct |
9 ms |
15444 KB |
Output is correct |
12 |
Correct |
9 ms |
15456 KB |
Output is correct |
13 |
Correct |
9 ms |
15564 KB |
Output is correct |
14 |
Correct |
9 ms |
15444 KB |
Output is correct |
15 |
Correct |
8 ms |
15444 KB |
Output is correct |
16 |
Correct |
10 ms |
15444 KB |
Output is correct |
17 |
Correct |
9 ms |
15568 KB |
Output is correct |
18 |
Correct |
8 ms |
15332 KB |
Output is correct |
19 |
Correct |
9 ms |
15444 KB |
Output is correct |
20 |
Correct |
9 ms |
15456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
34116 KB |
Output is correct |
2 |
Correct |
70 ms |
24840 KB |
Output is correct |
3 |
Correct |
182 ms |
30624 KB |
Output is correct |
4 |
Correct |
104 ms |
27720 KB |
Output is correct |
5 |
Correct |
819 ms |
79432 KB |
Output is correct |
6 |
Correct |
661 ms |
68488 KB |
Output is correct |
7 |
Correct |
290 ms |
52476 KB |
Output is correct |
8 |
Correct |
333 ms |
48288 KB |
Output is correct |
9 |
Correct |
358 ms |
48352 KB |
Output is correct |
10 |
Correct |
279 ms |
46912 KB |
Output is correct |
11 |
Correct |
119 ms |
31692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
8 ms |
15080 KB |
Output is correct |
3 |
Correct |
8 ms |
15108 KB |
Output is correct |
4 |
Correct |
7 ms |
15188 KB |
Output is correct |
5 |
Correct |
8 ms |
15188 KB |
Output is correct |
6 |
Correct |
7 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15324 KB |
Output is correct |
8 |
Correct |
8 ms |
15200 KB |
Output is correct |
9 |
Correct |
13 ms |
15752 KB |
Output is correct |
10 |
Correct |
10 ms |
15712 KB |
Output is correct |
11 |
Correct |
9 ms |
15444 KB |
Output is correct |
12 |
Correct |
9 ms |
15456 KB |
Output is correct |
13 |
Correct |
9 ms |
15564 KB |
Output is correct |
14 |
Correct |
9 ms |
15444 KB |
Output is correct |
15 |
Correct |
8 ms |
15444 KB |
Output is correct |
16 |
Correct |
10 ms |
15444 KB |
Output is correct |
17 |
Correct |
9 ms |
15568 KB |
Output is correct |
18 |
Correct |
8 ms |
15332 KB |
Output is correct |
19 |
Correct |
9 ms |
15444 KB |
Output is correct |
20 |
Correct |
9 ms |
15456 KB |
Output is correct |
21 |
Correct |
173 ms |
34116 KB |
Output is correct |
22 |
Correct |
70 ms |
24840 KB |
Output is correct |
23 |
Correct |
182 ms |
30624 KB |
Output is correct |
24 |
Correct |
104 ms |
27720 KB |
Output is correct |
25 |
Correct |
819 ms |
79432 KB |
Output is correct |
26 |
Correct |
661 ms |
68488 KB |
Output is correct |
27 |
Correct |
290 ms |
52476 KB |
Output is correct |
28 |
Correct |
333 ms |
48288 KB |
Output is correct |
29 |
Correct |
358 ms |
48352 KB |
Output is correct |
30 |
Correct |
279 ms |
46912 KB |
Output is correct |
31 |
Correct |
119 ms |
31692 KB |
Output is correct |
32 |
Correct |
113 ms |
27072 KB |
Output is correct |
33 |
Correct |
128 ms |
28576 KB |
Output is correct |
34 |
Correct |
366 ms |
49120 KB |
Output is correct |
35 |
Correct |
255 ms |
40464 KB |
Output is correct |
36 |
Correct |
270 ms |
43676 KB |
Output is correct |
37 |
Correct |
312 ms |
45496 KB |
Output is correct |
38 |
Correct |
283 ms |
51768 KB |
Output is correct |
39 |
Correct |
144 ms |
30020 KB |
Output is correct |
40 |
Correct |
342 ms |
49644 KB |
Output is correct |
41 |
Correct |
364 ms |
49888 KB |
Output is correct |
42 |
Correct |
454 ms |
58712 KB |
Output is correct |
43 |
Correct |
184 ms |
36416 KB |
Output is correct |
44 |
Correct |
364 ms |
40976 KB |
Output is correct |
45 |
Correct |
295 ms |
45760 KB |
Output is correct |
46 |
Correct |
260 ms |
45720 KB |
Output is correct |
47 |
Correct |
275 ms |
45604 KB |
Output is correct |
48 |
Correct |
857 ms |
81040 KB |
Output is correct |