This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 200 + 2;
const int M = 5e4 + 2;
const ll linf = 1e18;
ll dist[N][N], di[N];
set<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], prv[N], n, m;
void Dijkstra(int x) {
for (int i = 0; i < n; i++) di[i] = linf;
vector<int> was(n);
priority_queue<pair<ll, int>> pq;
di[x] = 0;
pq.push({0, x});
while (!pq.empty()) {
ll v = -pq.top().first;
int s = pq.top().second;
pq.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (auto zz : g[s]) {
int u = zz.first; int i = zz.second;
if (di[u] > v + c[i]) {
di[u] = v + c[i];
pq.push({-di[u], u});
prv[u] = i;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i] -= 1; v[i] -= 1;
smin(dist[u[i]][v[i]],(ll) c[i]);
g[u[i]].insert({v[i], i});
}
for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(0);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(n - 1);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
ll ans = linf;
if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0];
for (int i = 0; i < m; i++) {
if (is[i] == 0) {
ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]);
ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]);
if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]);
continue;
}
g[u[i]].erase({v[i], i});
g[v[i]].insert({u[i], i});
ll ret = d[i];
Dijkstra(0);
if (di[n - 1] == linf) {
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
continue;
}
ret += di[n - 1];
Dijkstra(n - 1);
if (di[0] == linf) {
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
continue;
}
ret += di[0];
smin(ans, ret);
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
}
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
# | 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... |