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 lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(x...) void(0)
#define owo ios::sync_with_stdio(false), cin.tie(0)
#endif
const int N = 205, M = 50001;
array <int, 4> E[M];
vector <int> adj[N];
int n;
lli build(int s, int t) {
vector <lli> dis(n, 1ll << 60);
vector <int> rt(n, -1);
dis[s] = 0;
priority_queue <pair <lli, int>, vector <pair <lli, int>>, greater <pair <lli, int>>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
lli d; int v; tie(d, v) = pq.top(); pq.pop();
if (d > dis[v])
continue;
for (int id : adj[v]) {
int u = E[id][0] ^ E[id][1] ^ v;
if (dis[u] > d + E[id][2])
dis[u] = d + E[id][2], rt[u] = v, pq.emplace(dis[u], u);
}
}
return dis[t];
}
int main () {
owo;
int m;
cin >> n >> m;
int s = 0, t = n - 1;
for (int i = 0, u, v, c, d; i < m; ++i) {
cin >> u >> v >> c >> d, --u, --v;
E[i] = {u, v, c, d};
adj[u].pb(i);
}
lli ans = build(s, t) + build(t, s);
for (int i = 0; i < m; ++i) {
int u = E[i][0], v = E[i][1];
adj[u].erase(find(all(adj[u]), i)), adj[v].pb(i);
ans = min(ans, build(s, t) + build(t, s) + E[i][3]);
adj[u].pb(i), adj[v].erase(find(all(adj[v]), i));
}
if (ans >= 1ll << 40)
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... |