#include <iostream>
#include<set>
#include<map>
#include<vector>
#define int long long
#define inf 1e15
using namespace std;
vector<vector<int>> g[205];
vector<int> path(int s, int n) {
set<pair<int, int>> q;
vector<int> dist(n + 1, inf);
q.insert({ 0, s }), dist[s] = 0;
while (q.size()) {
pair<int, int> f = *q.begin();
q.erase(q.begin());
for (auto y : g[f.second]) {
int i = y[0], x = y[1];
if (dist[i] > (x + f.first)) {
if (dist[i] != inf) q.erase({ dist[i], i });
dist[i] = (x + f.first);
q.insert({ dist[i], i });
}
}
}
return dist;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
vector<vector<int>> e;
for (int i = 0; i < m; ++i) {
int u, v, c, d; cin >> u >> v >> c >> d;
g[u].push_back({ v, c, d });
e.push_back({ u, v, c, d });
}
vector<vector<int>> d(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; ++i)
d[i] = path(i, n);
int ans = d[1][n] + d[n][1];
for (auto x : e) {
int u = x[0], v = x[1];
int c1 = min(d[1][n], d[1][v] + d[u][n] + x[2]);
int c2 = min(d[n][1], d[n][v] + d[u][1] + x[2]);
ans = min(ans, c1 + c2 + x[3]);
}
cout << ans;
}
# | 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... |