#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define mp make_pair
#define Sz(x) (int)x.size()
using namespace std;
const int N = 205;
const int inf = 1e18;
int n, m;
vector <pair<int,int>> g[N];
struct node {
int u, v, c, d;
};
int get(int s, int f) {
vector <int> d(n + 1, inf);
priority_queue <pair<int,int>> st;
st.push(mp(0, s));
d[s] = 0;
while (!st.empty()) {
pair <int,int> p = st.top();
st.pop();
int dist = -p.fi, v = p.se;
if (d[v] < dist) continue;
for (auto [u, D] : g[v]) {
if (d[u] > d[v] + D) {
d[u] = d[v] + D;
st.push(mp(-d[u], u));
}
}
}
return d[f];
}
void solve() {
cin >> n >> m;
vector <node> edges;
for (int i = 1; i <= m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
edges.push_back({u, v, c, d});
}
int res = inf;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int u = edges[j].u, v = edges[j].v, c = edges[j].c, d = edges[j].d;
if (i == j) {
g[v].push_back(mp(u, c + d));
}
else {
g[u].push_back(mp(v, c));
}
}
int ans = get(1, n) + get(n, 1);
if (ans < inf) res = min(res, ans);
for (int j = 1; j <= n; j++) {
g[j].clear();
}
}
if (res == inf) cout << -1;
else cout << res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
// cout << '\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... |