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 EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = ll(4e18) + ll(2e15);
const double EPS = 1e-9;
static int LamyIsCute = []() {
EmiliaMyWife
return 48763;
}();
const int N = 207;
vector<pair<int, int>> edge[N];
void dijk(int s, vector<int> &dis) {
dis[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if(d > dis[u])
continue;
for(const auto &[v, c] : edge[u]) {
if(dis[v] > dis[u] + c) {
dis[v] = dis[u] + c;
pq.push({dis[v], v});
}
}
}
}
signed main() {
int n, m;
cin >> n >> m;
vector<tuple<int, int, int, int>> edges;
for(int i = 0; i < m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
if(i % 2 == 0) {
edge[u].push_back({v, c});
edges.push_back({u, v, c, d});
}
}
vector<vector<int>> dis(n + 1, vector<int>(n + 1, INF));
for(int i = 1; i <= n; i++)
dijk(i, dis[i]);
int ans = INT_MAX;
if(dis[1][n] < INF && dis[n][1] < INF)
ans = min(ans, dis[1][n] + dis[n][1]);
for(const auto &[u, v, c, d] : edges) {
ans = min<ll>({
ans,
(dis[1][v] < INF && dis[u][n] < INF && dis[u][1] < INF ? dis[1][v] + dis[u][n] + c + dis[n][1] + d : INT_MAX),
(dis[1][v] < INF && dis[u][n] < INF && dis[n][v] < INF && dis[u][1] < INF ? dis[1][v] + dis[u][n] + c + dis[n][v] + dis[u][1] + c + d : INT_MAX),
(dis[1][n] < INF && dis[n][v] < INF && dis[u][1] < INF ? dis[1][n] + dis[n][v] + dis[u][1] + c + d : INT_MAX),
});
}
if(ans == INT_MAX)
cout << "-1\n";
else
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... |