#include <iostream>
#include<set>
#include<map>
#include<vector>
#define int long long
using namespace std;
vector<vector<int>> g[205][2];
map<vector<int>, int> dist;
vector<vector<int>> e;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
e.push_back({}); //1 based idxing
for (int i = 1; i <= m; ++i) {
int u, v, c, d; cin >> u >> v >> c >> d;
g[u][0].push_back({ v, c ,d, i });
g[v][1].push_back({ u,c, d, i });
e.push_back({ u, v, c ,d });
}
set<vector<int>> s; //{dist+cost, node, path, rev. edg.}
s.insert({ 0, 1, 0, 0 }), dist[{1, 0, 0 }] = 0;
while (s.size()) {
vector<int> f = *s.begin();
s.erase(s.begin());
if (f[2] && (f[1] == 1)) { //complete!
cout << f[0] << endl;
return 0;
}
if (f[1] == n) f[2] = 1; //1 way complete!
for (auto x : g[f[1]][1]) { //in edgs.
f[3] = x[3];
if (dist.find({ x[0], f[2], f[3] }) != dist.end()) {
if (dist[{ x[0], f[2], f[3] }] < (f[0] + x[1]))
continue; //worse path
s.erase({ dist[{ x[0], f[2], f[3] }], x[0], f[2], f[3] });
}
dist[{ x[0], f[2], f[3] }] = (f[0] + x[1]);
s.insert({ dist[{ x[0], f[2], f[3] }], x[0], f[2], f[3] });
f[3] = 0;
}
if (f[3] == 0) {
for (auto x : g[f[1]][1]) { //in edgs.
f[3] = x[3];
if (dist.find({ x[0], f[2], f[3] }) != dist.end()) {
if (dist[{ x[0], f[2], f[3] }] < (f[0] + x[1] + x[2]))
continue; //worse path
s.erase({ dist[{ x[0], f[2], f[3] }], x[0], f[2], f[3] });
}
dist[{ x[0], f[2], f[3] }] = (f[0] + x[1] + x[2]);
s.insert({ dist[{ x[0], f[2], f[3] }], 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... |