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 int unsigned int
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define endl "\n"
const int inf = 2e9;
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 201, M = 50001;
int n, m, aa[M], bb[M], cc[M], dd[M], dis[N], vis[N], dist[N][N], isShort[M];
set<pii> g[N];
void dijkstra(int source) {
priority_queue<pii, vector<pii>, greater<>> q;
vi vis(n+1, 0), dis(n+1, inf), par(n+1), idx(n+1);
q.push({0, source});
dis[source] = 0;
while (!q.empty()) {
pii f = q.top(); q.pop();
int src = f.s;
if (vis[src]) continue;
vis[src] = true;
for (auto child : g[src]) {
int ch = child.f, wt = cc[child.s];
if (dis[src] < inf && dis[ch] > dis[src] + wt) {
par[ch] = src;
idx[ch] = child.s;
dis[ch] = dis[src] + wt;
q.push({dis[ch], ch});
}
}
}
int x = (source==1?n:1);
while (par[x]) {
isShort[idx[x]] = true;
x = par[x];
}
}
int get_dist(int s, int t) {
for (int i = 1; i <= n; i++) dis[i] = inf, vis[i] = 0;
priority_queue<pii, vector<pii>, greater<>> q;
q.push({0, s});
dis[s] = 0;
while (!q.empty()) {
pii f = q.top(); q.pop();
int src = f.s;
if (vis[src]) continue;
vis[src] = true;
for (auto child : g[src]) {
int ch = child.f, wt = cc[child.s];
if (dis[src] < inf && dis[ch] > dis[src] + wt) {
dis[ch] = dis[src] + wt;
q.push({dis[ch], ch});
}
}
}
return dis[t];
}
void sol() {
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) dist[i][j] = inf;
for (int i = 1; i <= m; i++) {
cin >> aa[i] >> bb[i] >> cc[i] >> dd[i];
dist[aa[i]][bb[i]] = min(dist[aa[i]][bb[i]], cc[i]);
g[aa[i]].insert({bb[i], i});
}
dijkstra(1);
dijkstra(n);
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (dist[i][k] < inf && dist[k][j] < inf)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = inf;
if (dist[1][n] < inf && dist[n][1] < inf) ans = dist[1][n] + dist[n][1];
for (int i = 1; i <= m; i++) {
int a = aa[i], b = bb[i], c = cc[i], d = dd[i];
if (isShort[i]) {
g[a].erase(g[a].find({b, i}));
g[b].insert({a, i});
int x = get_dist(1, n), y = get_dist(n, 1);
if (x < inf && y < inf)
ans = min(ans, x + y + d);
g[b].erase(g[b].find({a, i}));
g[a].insert({b, i});
}
else {
int A = dist[1][n];
int B = dist[n][1];
if (dist[1][b] < inf && dist[a][n] < inf) A = min(A, dist[1][b] + c + dist[a][n]);
if (dist[n][b] < inf && dist[a][1] < inf) B = min(B, dist[n][b] + c + dist[a][1]);
if (A < inf && B < inf)
ans = min(ans, A + B + d);
}
}
if (ans == inf) cout << "-1" << endl;
else cout << ans << endl;
}
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
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... |