#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define SZ(x) ((int)((x).size()))
#define debug(x) cerr << #x << " = " << x << '\n'
const int mxn = 205, mxm = 5e4+5;
array<int,4> ed[mxm];
int n,m, ex[mxm];
vector<array<int,3>> adj[mxn], radj[mxn];
struct graph {
ll dis[mxn], vis[mxn], pred[mxn], on[mxm];
void dij(int rev, int s) { //to, cost, idx
for (int q = 0; q < mxn; q++) {
dis[q] = pred[q] = INT_MAX;
vis[q] = 0;
}
for (int q = 0; q < mxm; q++) on[q] = 0;
dis[s] = 0;
for (int q = 0; q < n; q++) {
int v = -1;
for (int w = 1; w <= n; w++) {
if (!vis[w] && (v == -1 || dis[w] < dis[v])) v = w;
}
if (v == -1 || dis[v] == INT_MAX) break;
vis[v] = 1;
if (!rev) {
for (int w = 0; w < SZ(adj[v]); w++) {
if (!ex[adj[v][w][2]]) continue;
if (dis[v]+adj[v][w][1] < dis[adj[v][w][0]]) {
dis[adj[v][w][0]] = dis[v]+adj[v][w][1];
pred[adj[v][w][0]] = adj[v][w][2];
}
}
} else {
for (int w = 0; w < SZ(radj[v]); w++) {
if (!ex[radj[v][w][2]]) continue;
if (dis[v]+radj[v][w][1] < dis[radj[v][w][0]]) {
dis[radj[v][w][0]] = dis[v]+radj[v][w][1];
pred[radj[v][w][0]] = radj[v][w][2];
}
}
}
}
for (int q = 1; q <= n; q++) if (pred[q] != INT_MAX) on[pred[q]] = 1;
}
} g[5];
ll get(int x, int y, int i) {
if (x == 1) {
if (g[0].on[i]) {
g[4].dij(0,1);
return g[4].dis[y];
} else return g[0].dis[y];
} else if (x == n) {
if (g[2].on[i]) {
g[4].dij(0,n);
return g[4].dis[y];
} else return g[2].dis[y];
} else if (y == 1) {
if (g[1].on[i]) {
g[4].dij(1,1);
return g[4].dis[x];
} else return g[1].dis[x];
} else {
if (g[3].on[i]) {
g[4].dij(1,n);
return g[4].dis[x];
} else return g[3].dis[x];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int q = 0; q < m; q++) {
for (int w = 0; w < 4; w++) {
cin >> ed[q][w];
}
ex[q] = 1;
adj[ed[q][0]].push_back({ed[q][1],ed[q][2],q});
radj[ed[q][1]].push_back({ed[q][0],ed[q][2],q});
}
g[0].dij(0,1);
g[1].dij(1,1);
g[2].dij(0,n);
g[3].dij(1,n);
ll ans = INT_MAX;
ans = min(ans, g[0].dis[n]+g[2].dis[1]);
for (int q = 0; q < m; q++) {
ex[q] = 0;
ll a = min(get(1,n,q),get(1,ed[q][1],q)+get(ed[q][0],n,q)+ed[q][2]);
ll b = min(get(n,1,q),get(n,ed[q][1],q)+get(ed[q][0],1,q)+ed[q][2]);
ans = min(ans, a+b+ed[q][3]);
ex[q] = 1;
}
if (ans == INT_MAX) cout << "-1\n";
else cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
2360 KB |
Output is correct |
2 |
Correct |
6 ms |
2252 KB |
Output is correct |
3 |
Correct |
143 ms |
2252 KB |
Output is correct |
4 |
Correct |
151 ms |
2252 KB |
Output is correct |
5 |
Correct |
7 ms |
2392 KB |
Output is correct |
6 |
Correct |
12 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
1 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2252 KB |
Output is correct |
10 |
Correct |
158 ms |
2372 KB |
Output is correct |
11 |
Correct |
152 ms |
2252 KB |
Output is correct |
12 |
Correct |
153 ms |
2380 KB |
Output is correct |
13 |
Correct |
45 ms |
2360 KB |
Output is correct |
14 |
Correct |
96 ms |
2360 KB |
Output is correct |
15 |
Correct |
97 ms |
2252 KB |
Output is correct |
16 |
Correct |
100 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
302 ms |
5260 KB |
Output is correct |
2 |
Correct |
303 ms |
5288 KB |
Output is correct |
3 |
Correct |
335 ms |
5272 KB |
Output is correct |
4 |
Correct |
144 ms |
2380 KB |
Output is correct |
5 |
Correct |
142 ms |
2252 KB |
Output is correct |
6 |
Correct |
23 ms |
2252 KB |
Output is correct |
7 |
Correct |
4 ms |
2252 KB |
Output is correct |
8 |
Correct |
1 ms |
2252 KB |
Output is correct |
9 |
Correct |
100 ms |
5204 KB |
Output is correct |
10 |
Correct |
96 ms |
5196 KB |
Output is correct |
11 |
Correct |
232 ms |
5304 KB |
Output is correct |
12 |
Correct |
213 ms |
5316 KB |
Output is correct |
13 |
Correct |
202 ms |
5216 KB |
Output is correct |
14 |
Correct |
202 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
2380 KB |
Output is correct |
2 |
Correct |
31 ms |
2252 KB |
Output is correct |
3 |
Correct |
349 ms |
4456 KB |
Output is correct |
4 |
Correct |
15 ms |
2332 KB |
Output is correct |
5 |
Correct |
385 ms |
5200 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
1 ms |
2252 KB |
Output is correct |
8 |
Correct |
127 ms |
5200 KB |
Output is correct |
9 |
Correct |
138 ms |
5212 KB |
Output is correct |
10 |
Correct |
258 ms |
5188 KB |
Output is correct |
11 |
Correct |
248 ms |
5256 KB |
Output is correct |
12 |
Correct |
261 ms |
5276 KB |
Output is correct |
13 |
Correct |
1 ms |
2252 KB |
Output is correct |
14 |
Correct |
1 ms |
2252 KB |
Output is correct |
15 |
Correct |
1 ms |
2252 KB |
Output is correct |
16 |
Correct |
1 ms |
2252 KB |
Output is correct |
17 |
Correct |
1 ms |
2252 KB |
Output is correct |
18 |
Correct |
1 ms |
2252 KB |
Output is correct |
19 |
Correct |
253 ms |
5252 KB |
Output is correct |
20 |
Correct |
253 ms |
5252 KB |
Output is correct |
21 |
Correct |
254 ms |
5320 KB |
Output is correct |
22 |
Correct |
251 ms |
5116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
2360 KB |
Output is correct |
2 |
Correct |
6 ms |
2252 KB |
Output is correct |
3 |
Correct |
143 ms |
2252 KB |
Output is correct |
4 |
Correct |
151 ms |
2252 KB |
Output is correct |
5 |
Correct |
7 ms |
2392 KB |
Output is correct |
6 |
Correct |
12 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
1 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2252 KB |
Output is correct |
10 |
Correct |
158 ms |
2372 KB |
Output is correct |
11 |
Correct |
152 ms |
2252 KB |
Output is correct |
12 |
Correct |
153 ms |
2380 KB |
Output is correct |
13 |
Correct |
45 ms |
2360 KB |
Output is correct |
14 |
Correct |
96 ms |
2360 KB |
Output is correct |
15 |
Correct |
97 ms |
2252 KB |
Output is correct |
16 |
Correct |
100 ms |
2360 KB |
Output is correct |
17 |
Correct |
302 ms |
5260 KB |
Output is correct |
18 |
Correct |
303 ms |
5288 KB |
Output is correct |
19 |
Correct |
335 ms |
5272 KB |
Output is correct |
20 |
Correct |
144 ms |
2380 KB |
Output is correct |
21 |
Correct |
142 ms |
2252 KB |
Output is correct |
22 |
Correct |
23 ms |
2252 KB |
Output is correct |
23 |
Correct |
4 ms |
2252 KB |
Output is correct |
24 |
Correct |
1 ms |
2252 KB |
Output is correct |
25 |
Correct |
100 ms |
5204 KB |
Output is correct |
26 |
Correct |
96 ms |
5196 KB |
Output is correct |
27 |
Correct |
232 ms |
5304 KB |
Output is correct |
28 |
Correct |
213 ms |
5316 KB |
Output is correct |
29 |
Correct |
202 ms |
5216 KB |
Output is correct |
30 |
Correct |
202 ms |
5456 KB |
Output is correct |
31 |
Correct |
145 ms |
2380 KB |
Output is correct |
32 |
Correct |
31 ms |
2252 KB |
Output is correct |
33 |
Correct |
349 ms |
4456 KB |
Output is correct |
34 |
Correct |
15 ms |
2332 KB |
Output is correct |
35 |
Correct |
385 ms |
5200 KB |
Output is correct |
36 |
Correct |
2 ms |
2252 KB |
Output is correct |
37 |
Correct |
1 ms |
2252 KB |
Output is correct |
38 |
Correct |
127 ms |
5200 KB |
Output is correct |
39 |
Correct |
138 ms |
5212 KB |
Output is correct |
40 |
Correct |
258 ms |
5188 KB |
Output is correct |
41 |
Correct |
248 ms |
5256 KB |
Output is correct |
42 |
Correct |
261 ms |
5276 KB |
Output is correct |
43 |
Correct |
1 ms |
2252 KB |
Output is correct |
44 |
Correct |
1 ms |
2252 KB |
Output is correct |
45 |
Correct |
1 ms |
2252 KB |
Output is correct |
46 |
Correct |
1 ms |
2252 KB |
Output is correct |
47 |
Correct |
1 ms |
2252 KB |
Output is correct |
48 |
Correct |
1 ms |
2252 KB |
Output is correct |
49 |
Correct |
253 ms |
5252 KB |
Output is correct |
50 |
Correct |
253 ms |
5252 KB |
Output is correct |
51 |
Correct |
254 ms |
5320 KB |
Output is correct |
52 |
Correct |
251 ms |
5116 KB |
Output is correct |
53 |
Correct |
312 ms |
5136 KB |
Output is correct |
54 |
Correct |
299 ms |
5188 KB |
Output is correct |
55 |
Correct |
310 ms |
5172 KB |
Output is correct |
56 |
Correct |
160 ms |
2380 KB |
Output is correct |
57 |
Correct |
147 ms |
2356 KB |
Output is correct |
58 |
Correct |
325 ms |
4428 KB |
Output is correct |
59 |
Correct |
299 ms |
4500 KB |
Output is correct |
60 |
Correct |
268 ms |
4468 KB |
Output is correct |
61 |
Correct |
262 ms |
4456 KB |
Output is correct |
62 |
Correct |
270 ms |
4524 KB |
Output is correct |
63 |
Correct |
264 ms |
4392 KB |
Output is correct |
64 |
Correct |
411 ms |
4668 KB |
Output is correct |
65 |
Correct |
376 ms |
4684 KB |
Output is correct |
66 |
Correct |
354 ms |
4704 KB |
Output is correct |
67 |
Correct |
23 ms |
4148 KB |
Output is correct |
68 |
Correct |
96 ms |
5148 KB |
Output is correct |
69 |
Correct |
93 ms |
5208 KB |
Output is correct |
70 |
Correct |
206 ms |
5316 KB |
Output is correct |
71 |
Correct |
206 ms |
5188 KB |
Output is correct |
72 |
Correct |
211 ms |
5188 KB |
Output is correct |
73 |
Correct |
261 ms |
5272 KB |
Output is correct |
74 |
Correct |
206 ms |
5244 KB |
Output is correct |