#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct Edge {
ll a, b, c, d;
};
ll n, m;
ll M = 210, inf = 1e17;
vector<vector<ll>> dist(M, vector<ll>(M, inf));
vector<Edge> e;
void floyd() {
for(int k=1; k<=n; ++k) {
for(int i=1; i<=n; ++i) {
for(int j=1; j<=n; ++j) {
if(dist[i][k] < inf && dist[k][j] < inf) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; ++i) dist[i][i] = 0;
for(ll i=0; i<m; ++i) {
Edge x;
cin >> x.a >> x.b >> x.c >> x.d;
dist[x.a][x.b] = min(dist[x.a][x.b], x.c);
e.emplace_back(x);
}
floyd();
ll ans = dist[1][n] + dist[n][1];
for(auto[a, b, c, d] : e) {
ll to = min(dist[1][n], dist[1][b] + c + d + dist[a][n]);
ll from = min(dist[n][1], dist[n][b] + c + d + dist[a][1]);
ans = min(ans, to+from);
}
if(ans >= inf) cout << "-1\n";
else cout << ans << "\n";
return 0;
}
# | 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... |