This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
struct Edge {
ll u, v, i;
};
multiset<pair<ll, ll>> adj[N];
ll c[M], d[M];
ll sssp(ll source, ll dest) {
vector<ll> dist(n + 1, 1e15);
dist[source] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({0, source});
while (!pq.empty()) {
ll node = pq.top().second, _dist = -pq.top().first;
pq.pop();
if (node == dest) {
return dist[node];
}
for (auto &nb : adj[node]) {
if (dist[nb.first] > _dist + c[nb.second]) {
dist[nb.first] = _dist + c[nb.second];
pq.push({-dist[nb.first], nb.first});
}
}
}
return 1e15;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (ll i = 1; i <= n; ++i) {
adj[i].clear();
}
vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, 1e15));
vector<Edge> edges(m);
for (ll i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
for (ll i = 0, u, v; i < m; ++i) {
cin >> u >> v >> c[i] >> d[i];
adj[u].insert({v, i});
edges[i].u = u, edges[i].v = v, edges[i].i = i;
dist[u][v] = min(dist[u][v], c[i]);
}
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
ll ans = min((ll)1e15, dist[1][n] + dist[n][1]);
sort(edges.begin(), edges.end(), [&dist](const Edge &e1, const Edge &e2) {
ll u1 = e1.u, u2 = e2.u, v1 = e1.v, v2 = e2.v, i1 = e1.i, i2 = e2.i;
ll d1 = min(dist[1][n], dist[1][v1] + c[i1] + dist[u1][n]) +
min(dist[n][1], dist[n][v1] + c[i1] + dist[u1][1]) + d[i1];
ll d2 = min(dist[1][n], dist[1][v2] + c[i2] + dist[u2][n]) +
min(dist[n][1], dist[n][v2] + c[i2] + dist[u2][1]) + d[i2];
return d1 < d2;
});
for (auto &[u, v, i] : edges) {
if (min(dist[1][n], dist[1][v] + c[i] + dist[u][n]) +
min(dist[n][1], dist[n][v] + c[i] + dist[u][1]) + d[i] <
ans) {
adj[u].erase(adj[u].find({v, i}));
adj[v].insert({u, i});
ans = min(ans, d[i] + sssp(1, n) + sssp(n, 1));
adj[v].erase(adj[v].find({u, i}));
adj[u].insert({v, i});
} else {
break;
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\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... |