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;
multiset<pair<ll, ll>> adj[N];
ll c[M], d[M];
pair<ll, ll> edges[M];
vector<ll> sssp(ll source) {
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();
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 dist;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (ll i = 1; i <= n; ++i) {
adj[i].clear();
}
for (ll i = 0, u, v; i < m; ++i) {
cin >> u >> v >> c[i] >> d[i];
adj[u].insert({v, i});
edges[i] = {u, v};
}
ll ans = 1e15;
for (ll i = 0; i < m; ++i) {
for (ll b = 0; b < 2; ++b) {
if (b) {
adj[edges[i].first].erase(
adj[edges[i].first].find({edges[i].second, i}));
adj[edges[i].second].insert({edges[i].first, i});
}
vector<ll> dist1 = sssp(1);
vector<ll> dist2 = sssp(n);
ans = min(ans, d[i] * b + dist1[n] + dist2[1]);
if (b) {
adj[edges[i].second].erase(
adj[edges[i].second].find({edges[i].first, i}));
adj[edges[i].first].insert({edges[i].second, i});
}
}
}
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... |