//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define ll long long
typedef pair<int, int> T;
set<T>cycle;
const int oo2 = 2e9+7;
const int mx = 202;
int n;
int pre[mx], dist[mx];
priority_queue<T>pq;
long long dijkstra(vector<vector<T>>&g, int from, int to = -1){
for (int i = 1; i<=n; i++) {
pre[i] = 0;
dist[i] = oo2;
}
dist[from] = 0;
pq.push({0, from});
while ((int)pq.size()){
auto [d, v] = pq.top();
pq.pop();
if (dist[v] < d) continue;
for (auto [x, c]: g[v]){
if (dist[x] > dist[v] + c){
pre[x] = v;
dist[x] = dist[v] + c;
pq.push({dist[x], x});
}
}
}
if (to >= 1){
int now = to;
int prev = -1;
while (now){
if (prev != -1) cycle.insert({now, prev});
prev = now;
now = pre[now];
}
return oo2;
}
return from == 1 ? dist[n] : dist[1];
}
void solve(){
int m; cin >> n >> m;
vector<vector<T>>g(n+1), gt(n+1);
vector<tuple<int, int, int, int>>edges;
vector d(n+1, vector<int>(n+1, oo2));
for (int i = 0; i<m; i++){
int a, b, c, dd; cin >> a >> b >> c >> dd;
g[a].emplace_back(b, c);
gt[b].emplace_back(a, c);
d[a][b] = min(d[a][b], c);
edges.emplace_back(a, b, c, dd);
}
for (int i = 1; i<=n; i++) d[i][i] = 0;
for (int k = 1; k<=n; k++){
for (int i = 1; i<=n; i++){
for (int j = 1; j<=n; j++){
if (d[i][k] == oo2 || d[k][j] == oo2) continue;
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
dijkstra(g, 1, n);
dijkstra(g, n, 1);
long long ans = (long long)d[1][n] + d[n][1];
debug(ans);
int k = 0;
debug(cycle);
for (auto &[a, b, c, dd]: edges){
if (cycle.find({a, b}) != cycle.end()){
debug(a, b);
auto [A, B, C, D] = edges[k];
for (int i = 0; i<(int)g[A].size(); i++){
if (g[A][i] == make_pair(B, C)){
swap(g[A][i], g[A].back());
break;
}
}
g[A].pop_back();
g[B].emplace_back(A, C);
long long tmp = (long long)dd + dijkstra(g, 1, 0) + dijkstra(g, n, 0);
debug(tmp);
g[B].pop_back();
g[A].emplace_back(B, C);
ans = min(ans, tmp);
} else {
ans = min(ans, (ll)dd + min((ll)d[1][n], (ll)d[1][b]+c+d[a][n]) + min((ll)d[n][1], (ll)d[n][b] + c + d[a][1]));
}
k++;
}
if (ans >= oo2) cout << "-1\n";
else cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
24 ms |
468 KB |
Output is correct |
4 |
Correct |
35 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
468 ms |
568 KB |
Output is correct |
11 |
Correct |
664 ms |
468 KB |
Output is correct |
12 |
Correct |
692 ms |
560 KB |
Output is correct |
13 |
Correct |
8 ms |
596 KB |
Output is correct |
14 |
Correct |
12 ms |
580 KB |
Output is correct |
15 |
Correct |
12 ms |
580 KB |
Output is correct |
16 |
Correct |
15 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
2556 KB |
Output is correct |
2 |
Correct |
239 ms |
2632 KB |
Output is correct |
3 |
Correct |
313 ms |
3784 KB |
Output is correct |
4 |
Correct |
33 ms |
596 KB |
Output is correct |
5 |
Correct |
19 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
31 ms |
3780 KB |
Output is correct |
10 |
Correct |
30 ms |
3660 KB |
Output is correct |
11 |
Correct |
316 ms |
3784 KB |
Output is correct |
12 |
Correct |
145 ms |
3752 KB |
Output is correct |
13 |
Correct |
169 ms |
3772 KB |
Output is correct |
14 |
Correct |
273 ms |
3836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
27 ms |
3056 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
32 ms |
3136 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
28 ms |
3196 KB |
Output is correct |
9 |
Correct |
27 ms |
3220 KB |
Output is correct |
10 |
Correct |
28 ms |
3124 KB |
Output is correct |
11 |
Correct |
37 ms |
3184 KB |
Output is correct |
12 |
Correct |
35 ms |
3292 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
26 ms |
3320 KB |
Output is correct |
20 |
Correct |
26 ms |
3236 KB |
Output is correct |
21 |
Correct |
29 ms |
3156 KB |
Output is correct |
22 |
Correct |
28 ms |
3128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
24 ms |
468 KB |
Output is correct |
4 |
Correct |
35 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
468 ms |
568 KB |
Output is correct |
11 |
Correct |
664 ms |
468 KB |
Output is correct |
12 |
Correct |
692 ms |
560 KB |
Output is correct |
13 |
Correct |
8 ms |
596 KB |
Output is correct |
14 |
Correct |
12 ms |
580 KB |
Output is correct |
15 |
Correct |
12 ms |
580 KB |
Output is correct |
16 |
Correct |
15 ms |
468 KB |
Output is correct |
17 |
Correct |
342 ms |
2556 KB |
Output is correct |
18 |
Correct |
239 ms |
2632 KB |
Output is correct |
19 |
Correct |
313 ms |
3784 KB |
Output is correct |
20 |
Correct |
33 ms |
596 KB |
Output is correct |
21 |
Correct |
19 ms |
564 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
31 ms |
3780 KB |
Output is correct |
26 |
Correct |
30 ms |
3660 KB |
Output is correct |
27 |
Correct |
316 ms |
3784 KB |
Output is correct |
28 |
Correct |
145 ms |
3752 KB |
Output is correct |
29 |
Correct |
169 ms |
3772 KB |
Output is correct |
30 |
Correct |
273 ms |
3836 KB |
Output is correct |
31 |
Correct |
9 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
456 KB |
Output is correct |
33 |
Correct |
27 ms |
3056 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
32 ms |
3136 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
28 ms |
3196 KB |
Output is correct |
39 |
Correct |
27 ms |
3220 KB |
Output is correct |
40 |
Correct |
28 ms |
3124 KB |
Output is correct |
41 |
Correct |
37 ms |
3184 KB |
Output is correct |
42 |
Correct |
35 ms |
3292 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
26 ms |
3320 KB |
Output is correct |
50 |
Correct |
26 ms |
3236 KB |
Output is correct |
51 |
Correct |
29 ms |
3156 KB |
Output is correct |
52 |
Correct |
28 ms |
3128 KB |
Output is correct |
53 |
Correct |
160 ms |
3568 KB |
Output is correct |
54 |
Correct |
320 ms |
3520 KB |
Output is correct |
55 |
Correct |
291 ms |
3564 KB |
Output is correct |
56 |
Correct |
24 ms |
468 KB |
Output is correct |
57 |
Correct |
18 ms |
468 KB |
Output is correct |
58 |
Correct |
570 ms |
3204 KB |
Output is correct |
59 |
Execution timed out |
1077 ms |
3272 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |