#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 200 + 2;
const int M = 5e4 + 2;
const ll linf = 1e18;
ll dist[N][N], di[N];
pair<int, int> prv[N];
vector<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], n, m;
void Dijkstra(int x) {
for (int i = 0; i < n; i++) di[i] = linf;
vector<int> was(n);
priority_queue<pair<ll, int>> pq;
di[x] = 0;
pq.push({0, x});
while (!pq.empty()) {
ll v = -pq.top().first;
int s = pq.top().second;
pq.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (auto zz : g[s]) {
int u = zz.first; int i = zz.second;
if (di[u] > v + c[i]) {
di[u] = v + c[i];
prv[u] = {s, i};
pq.push({-di[u], u});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i] -= 1; v[i] -= 1;
smin(dist[u[i]][v[i]],(ll) c[i]);
g[u[i]].push_back({v[i], i});
}
for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]);
Dijkstra(0);
if (di[n - 1] != linf) {
int x = n - 1;
while (x > 0) {
is[prv[x].second] = 1;
x = prv[x].first;
}
}
Dijkstra(n - 1);
if (di[0] != linf) {
int x = 0;
while (x < n - 1) {
is[prv[x].second] = 1;
x = prv[x].first;
}
}
ll ans = linf;
if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0];
for (int i = 0; i < m; i++) {
if (is[i] == 0) {
ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]);
ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]);
if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]);
continue;
}
for (int j = 0; j < n; j++) g[i].clear();
for (int j = 0; j < m; j++) {
if (i == j) g[v[j]].push_back({u[j], j});
else g[u[j]].push_back({v[j], j});
}
ll ret = d[i];
Dijkstra(0);
if (di[n - 1] == linf) continue;
ret += di[n - 1];
Dijkstra(n - 1);
if (di[0] == linf) continue;
ret += di[0];
smin(ans, ret);
}
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
724 KB |
Output is correct |
2 |
Correct |
10 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
836 KB |
Output is correct |
4 |
Correct |
10 ms |
828 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
6556 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
832 KB |
Output is correct |
2 |
Incorrect |
9 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
724 KB |
Output is correct |
2 |
Correct |
10 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
836 KB |
Output is correct |
4 |
Correct |
10 ms |
828 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |