/**
* 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;
};
auto reverse_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] : radj[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);
vector<int64_t> rev_from_dest = reverse_dijkstra(n - 1);
vector<int64_t> rev_from_source = reverse_dijkstra(0);
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]] + rev_from_dest[u[i]] + c[i], from_source[n - 1]), to = min(from_dest[v[i]] + rev_from_source[u[i]] + c[i], from_dest[0]);
if (from > inf || to > inf) {
continue;
}
mn = min(mn, from + d[i] + to);
}
cout << ((mn == inf) ? -1 : mn) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2392 KB |
Output is correct |
2 |
Correct |
14 ms |
3584 KB |
Output is correct |
3 |
Correct |
15 ms |
3672 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
15 ms |
3716 KB |
Output is correct |
10 |
Correct |
15 ms |
3716 KB |
Output is correct |
11 |
Correct |
15 ms |
3536 KB |
Output is correct |
12 |
Correct |
15 ms |
3676 KB |
Output is correct |
13 |
Correct |
15 ms |
3612 KB |
Output is correct |
14 |
Correct |
14 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
10 ms |
1828 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
13 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |