This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//thatsramen
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
const ll dx[8] = {1, 1, -1, -1, 2, 2, -2, -2}, dy[8] = {-2, 2, -2, 2, -1, 1, -1, 1};
const ld PI = 3.14159265359;
//Templates
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...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
unsyncIO(); setPrec();
if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
// #define TEST_CASES
const ll MAX = 1ll * 1000 * 1000 * 1000 * 1000 * 1000;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<tuple<int, int, int>>> e(n);
vector<set<int>> cols(n);
map<pi, ll> need, dist;
map<pi, bool> vis;
map<pi, pi> costs;
for (int i = 0; i < m; i++) {
int x, y, c, p;
cin >> x >> y >> c >> p;
x--; y--;
e[x].pb({y, c, p});
e[y].pb({x, c, p});
costs[{x, y}] = costs[{y, x}] = {c, p};
if (!need.count({x, c})) need[{x, c}] = 0;
if (!need.count({y, c})) need[{y, c}] = 0;
need[{x, c}] += p;
need[{y, c}] += p;
cols[x].insert(c);
cols[y].insert(c);
}
ll ans = MAX;
dist[{0, -1}] = 0;
costs[{0, -1}] = {0, 0};
priority_queue<tuple<ll, int, int>> que;
que.push({0, 0, -1});
while (!que.empty()) {
auto [w, v, fr] = que.top();
que.pop();
if (v == n - 1) {
ans = -w;
break;
}
if (vis.count({v, fr})) continue;
vis[{v, fr}] = true;
ll mn = MAX;
pi prev = costs[{fr, v}];
need[{v, prev.ft}] -= prev.sd;
for (int c : cols[v]) {
mins(mn, need[{v, c}]);
}
if ((int) cols.size() < m) mn = 0;
for (auto &[to, c, p] : e[v]) {
if (vis.count({to, v})) continue;
if (to == fr) continue;
ll add = min(mn + p, need[{v, c}] - p);
ll nw = dist[{v, fr}] + add;
if (!dist.count({to, v}) || dist[{to, v}] > nw) {
dist[{to, v}] = nw;
que.push({-nw, to, v});
}
}
need[{v, prev.ft}] += prev.sd;
}
if (ans == MAX) ans = -1;
cout << ans << '\n';
}
int main() {
setIO();
int tt = 1;
#ifdef TEST_CASES
cin >> tt;
#endif
while (tt--)
solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void setIn(std::string)':
Main.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void setOut(std::string)':
Main.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |