#include <iostream>
#include<set>
#include<map>
#include<vector>
#define int long long
const int inf = 1e17;
using namespace std;
vector<vector<int>> g[2][205];
vector<vector<int>> dist[2]; //path, node, rev. edg
int32_t main()
{
int n, m; cin >> n >> m;
dist[0].assign(n + 1, vector<int>(m + 1, inf));
dist[1].assign(n + 1, vector<int>(m + 1, inf));
for (int i = 1; i <= m; ++i) {
int u, v, c, d; cin >> u >> v >> c >> d;
g[0][u].push_back({ v, c ,d, i });
g[1][v].push_back({ u, c, d, i });
}
set<vector<int>> s; //{dist+cost, node, path, rev. edg.}
s.insert({ 0, 1, 0, 0 }), dist[0][1][0] = 0;
while (s.size()) {
vector<int> f = *s.begin();
s.erase(s.begin());
if (f[2] && (f[1] == 1)) { //complete!
cout << f[0];
return 0;
}
if (f[1] == n) f[2] = 1; //1 way complete!
for (auto x : g[0][f[1]]) { //out edgs.
if (f[3] != x[3]) { //this edg. wasnt rev.
int val = dist[f[2]][x[0]][f[3]];
if (val > (f[0] + x[1])) {
if (val != inf)
s.erase({ val, x[0], f[2], f[3] });
dist[f[2]][x[0]][f[3]] = (f[0] + x[1]);
s.insert({ (f[0] + x[1]), x[0], f[2], f[3] });
}
}
}
if (f[3] == 0) {
for (auto x : g[1][f[1]]) { //in edgs.
f[3] = x[3];
int val = dist[f[2]][x[0]][f[3]];
if (val > (f[0] + x[1] + x[2])) {
if (val != inf)
s.erase({ val, x[0], f[2], f[3] });
dist[f[2]][x[0]][f[3]] = (f[0] + x[1] + x[2]);
s.insert({ (f[0] + x[1] + x[2]), x[0], f[2], f[3] });
}
f[3] = 0;
}
}
}
cout << -1;
}
# | 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... |