This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: hashman
* created:
**/
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
constexpr int64_t inf = 1e18;
using P = pair<int64_t, int>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pair<int, int>>> adj(n), radj(n);
vector<int> u(m), v(m), c(m), d(m);
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i]--; v[i]--;
adj[u[i]].push_back(make_pair(v[i], c[i]));
radj[v[i]].push_back(make_pair(u[i], c[i]));
}
auto dijkstra = [&](int source) {
vector<int64_t> dist(n, inf);
dist[source] = 0;
priority_queue<P, vector<P>, greater<P>> pq;
pq.push(make_pair(0, source));
while (!pq.empty()) {
auto [expected, v] = pq.top();
pq.pop();
if (expected != dist[v]) {
continue;
}
for (auto [now, wt] : adj[v]) {
if (dist[now] > dist[v] + wt) {
dist[now] = dist[v] + wt;
pq.push(make_pair(dist[now], now));
}
}
}
return dist;
};
vector<int64_t> from_source = dijkstra(0);
vector<int64_t> from_dest = dijkstra(n - 1);
int64_t mn = inf;
mn = min(mn, from_source[n - 1] + from_dest[0]);
for (int i = 0; i < m; i++) {
int64_t from = min(from_source[v[i]] + from_dest[u[i]], from_source[n - 1]), to = min(from_dest[v[i]] + from_source[u[i]], from_dest[0]);
if (from > inf || to > inf) {
continue;
}
mn = min(mn, from + d[i] + to);
}
cout << ((mn == inf) ? -1 : mn) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |