#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];
set<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], prv[N], 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];
pq.push({-di[u], u});
prv[u] = i;
}
}
}
}
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]].insert({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]);
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(0);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(n - 1);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
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;
}
g[u[i]].erase({v[i], i});
g[v[i]].insert({u[i], i});
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);
g[v[i]].erase({u[i], i});
g[u[i]].insert({v[i], i});
}
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
724 KB |
Output is correct |
2 |
Correct |
10 ms |
596 KB |
Output is correct |
3 |
Correct |
29 ms |
852 KB |
Output is correct |
4 |
Correct |
29 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
692 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
28 ms |
724 KB |
Output is correct |
11 |
Correct |
18 ms |
856 KB |
Output is correct |
12 |
Correct |
32 ms |
724 KB |
Output is correct |
13 |
Correct |
11 ms |
744 KB |
Output is correct |
14 |
Correct |
17 ms |
744 KB |
Output is correct |
15 |
Incorrect |
12 ms |
740 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
539 ms |
3992 KB |
Output is correct |
2 |
Correct |
572 ms |
4020 KB |
Output is correct |
3 |
Correct |
523 ms |
3996 KB |
Output is correct |
4 |
Correct |
28 ms |
824 KB |
Output is correct |
5 |
Correct |
16 ms |
740 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
9 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
82 ms |
3896 KB |
Output is correct |
10 |
Correct |
109 ms |
3964 KB |
Output is correct |
11 |
Correct |
140 ms |
3920 KB |
Output is correct |
12 |
Correct |
353 ms |
4008 KB |
Output is correct |
13 |
Correct |
117 ms |
3980 KB |
Output is correct |
14 |
Incorrect |
89 ms |
3956 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
744 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
646 ms |
3244 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
717 ms |
3964 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
132 ms |
3916 KB |
Output is correct |
9 |
Correct |
162 ms |
3940 KB |
Output is correct |
10 |
Correct |
396 ms |
3964 KB |
Output is correct |
11 |
Correct |
215 ms |
3888 KB |
Output is correct |
12 |
Correct |
224 ms |
3908 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
526 ms |
3900 KB |
Output is correct |
20 |
Correct |
173 ms |
3964 KB |
Output is correct |
21 |
Correct |
195 ms |
3916 KB |
Output is correct |
22 |
Correct |
200 ms |
3916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
724 KB |
Output is correct |
2 |
Correct |
10 ms |
596 KB |
Output is correct |
3 |
Correct |
29 ms |
852 KB |
Output is correct |
4 |
Correct |
29 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
692 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
28 ms |
724 KB |
Output is correct |
11 |
Correct |
18 ms |
856 KB |
Output is correct |
12 |
Correct |
32 ms |
724 KB |
Output is correct |
13 |
Correct |
11 ms |
744 KB |
Output is correct |
14 |
Correct |
17 ms |
744 KB |
Output is correct |
15 |
Incorrect |
12 ms |
740 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |