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>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
vector<pair<ll, ll>> adj[N];
ll c[M], d[M];
pair<ll, ll> edges[M];
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));
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].emplace_back(v, i);
edges[i] = {u, v};
dist[u][v] = min(dist[u][v], c[i]);
}
for (ll a = 1; a <= n; ++a) {
for (ll b = 1; b <= n; ++b) {
for (ll c = 1; c <= n; ++c) {
dist[a][c] = min(dist[a][c], dist[a][b] + dist[b][c]);
}
}
}
set<ll> edges_to, edges_from;
// if (dist[1][n] != (ll)1e15) {
// for (ll node = 1; node != n;) {
// for (auto &nb : adj[node]) {
// if (c[nb.second] + dist[nb.first][n] == dist[node][n]) {
// edges_to.insert(nb.second);
// node = nb.first;
// }
// }
// }
// }
// if (dist[n][1] != (ll)1e15) {
// for (ll node = n; node != 1;) {
// for (auto &nb : adj[node]) {
// if (c[nb.second] + dist[nb.first][1] == dist[node][1]) {
// edges_from.insert(nb.second);
// node = nb.first;
// }
// }
// }
// }
ll ans = min((ll)1e15, dist[1][n] + dist[n][1]);
for (ll i = 0; i < m; ++i) {
ll u = edges[i].first, v = edges[i].second;
ll _ans = d[i];
ll d1 = dist[1][v] + c[i] + dist[u][n], d2 = dist[n][v] + c[i] + dist[u][1];
if (1 == u || n == v) {
d1 = 1e15;
}
if (u == n || 1 == v) {
d2 = 1e15;
}
if (edges_to.count(i)) {
_ans += d1;
} else {
_ans += min(dist[1][n], d1);
}
if (edges_from.count(i)) {
_ans += d2;
} else {
_ans += min(dist[n][1], d2);
}
ans = min(ans, _ans);
}
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... |