#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define endl "\n"
const int inf = 1e15;
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 201, M = 50001;
int n, m, aa[M], bb[M], cc[M], dd[M], dis[N], vis[N], dist[N][N], isShort[M];
// set<pii> g[N];
vector<pair<int, int>> g[N];
void dijkstra(int source) {
priority_queue<pii, vector<pii>, greater<>> q;
vi vis(n+1, 0), dis(n+1, inf), par(n+1), idx(n+1);
q.push({0, source});
dis[source] = 0;
while (!q.empty()) {
pii f = q.top(); q.pop();
int src = f.s;
if (vis[src]) continue;
vis[src] = true;
for (auto child : g[src]) {
int ch = child.f, wt = cc[child.s];
if (dis[ch] > dis[src] + wt) {
par[ch] = src;
idx[ch] = child.s;
dis[ch] = dis[src] + wt;
q.push({dis[ch], ch});
}
}
}
int x = (source==1?n:1);
while (par[x]) {
isShort[idx[x]] = true;
x = par[x];
}
}
int get_dist(int s, int t) {
vi D(n+1, inf);
priority_queue<pii, vector<pii>, greater<pii>> pq;
D[s] = 0;
pq.emplace(0, s);
while (pq.size()) {
pii temp = pq.top(); int x = temp.f, u = temp.s;
pq.pop();
if (D[u] == x) {
for (pii child : g[u]) {
int v = child.f, wt = cc[child.s];
if (D[v] > x + wt) {
pq.emplace(D[v] = x + wt, v);
}
}
}
}
return D[t];
}
void add(int u, int v, int id) {
g[u].emplace_back(v, id);
}
void del(int u, int v, int id) {
for (auto &x : g[u]) {
if (x == make_pair(v, id)) {
swap(x, g[u].back());
g[u].pop_back();
break;
}
}
}
void sol() {
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) dist[i][j] = inf;
for (int i = 1; i <= m; i++) {
cin >> aa[i] >> bb[i] >> cc[i] >> dd[i];
dist[aa[i]][bb[i]] = min(dist[aa[i]][bb[i]], cc[i]);
// g[aa[i]].insert({bb[i], i});
add(aa[i], bb[i], i);
}
dijkstra(1);
dijkstra(n);
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = min(inf, dist[1][n] + dist[n][1]);
for (int i = 1; i <= m; i++) {
int a = aa[i], b = bb[i], c = cc[i], d = dd[i];
if (isShort[i]) {
del(a, b, i);
add(b, a, i);
// g[a].erase(g[a].find({b, i}));
// g[b].insert({a, i});
ans = min(ans, get_dist(1, n) + get_dist(n, 1) + d);
// g[b].erase(g[b].find({a, i}));
// g[a].insert({b, i});
del(b, a, i);
add(a, b, i);
}
else {
int A = min(dist[1][n], dist[1][b] + c + dist[a][n]);
int B = min(dist[n][1], dist[n][b] + c + dist[a][1]);
ans = min(ans, A + B + d);
}
}
cout << (ans==inf?-1:ans) << endl;
}
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
2392 KB |
Output is correct |
3 |
Correct |
8 ms |
2396 KB |
Output is correct |
4 |
Correct |
8 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
7 ms |
2528 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
20 ms |
2576 KB |
Output is correct |
11 |
Correct |
23 ms |
2404 KB |
Output is correct |
12 |
Correct |
22 ms |
2396 KB |
Output is correct |
13 |
Correct |
10 ms |
2392 KB |
Output is correct |
14 |
Correct |
8 ms |
2396 KB |
Output is correct |
15 |
Correct |
8 ms |
2568 KB |
Output is correct |
16 |
Correct |
8 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
3932 KB |
Output is correct |
2 |
Correct |
22 ms |
3928 KB |
Output is correct |
3 |
Correct |
22 ms |
3676 KB |
Output is correct |
4 |
Correct |
8 ms |
2392 KB |
Output is correct |
5 |
Correct |
8 ms |
2396 KB |
Output is correct |
6 |
Correct |
9 ms |
2544 KB |
Output is correct |
7 |
Correct |
7 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
27 ms |
3672 KB |
Output is correct |
10 |
Correct |
20 ms |
3676 KB |
Output is correct |
11 |
Correct |
22 ms |
3904 KB |
Output is correct |
12 |
Correct |
22 ms |
3928 KB |
Output is correct |
13 |
Correct |
21 ms |
3932 KB |
Output is correct |
14 |
Correct |
22 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2392 KB |
Output is correct |
2 |
Correct |
7 ms |
2592 KB |
Output is correct |
3 |
Correct |
17 ms |
3420 KB |
Output is correct |
4 |
Correct |
8 ms |
2396 KB |
Output is correct |
5 |
Correct |
19 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
19 ms |
3676 KB |
Output is correct |
9 |
Correct |
18 ms |
3752 KB |
Output is correct |
10 |
Correct |
22 ms |
3728 KB |
Output is correct |
11 |
Correct |
19 ms |
3676 KB |
Output is correct |
12 |
Correct |
19 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
19 ms |
3932 KB |
Output is correct |
20 |
Correct |
19 ms |
3676 KB |
Output is correct |
21 |
Correct |
19 ms |
3676 KB |
Output is correct |
22 |
Correct |
20 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
2392 KB |
Output is correct |
3 |
Correct |
8 ms |
2396 KB |
Output is correct |
4 |
Correct |
8 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
7 ms |
2528 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
20 ms |
2576 KB |
Output is correct |
11 |
Correct |
23 ms |
2404 KB |
Output is correct |
12 |
Correct |
22 ms |
2396 KB |
Output is correct |
13 |
Correct |
10 ms |
2392 KB |
Output is correct |
14 |
Correct |
8 ms |
2396 KB |
Output is correct |
15 |
Correct |
8 ms |
2568 KB |
Output is correct |
16 |
Correct |
8 ms |
2392 KB |
Output is correct |
17 |
Correct |
21 ms |
3932 KB |
Output is correct |
18 |
Correct |
22 ms |
3928 KB |
Output is correct |
19 |
Correct |
22 ms |
3676 KB |
Output is correct |
20 |
Correct |
8 ms |
2392 KB |
Output is correct |
21 |
Correct |
8 ms |
2396 KB |
Output is correct |
22 |
Correct |
9 ms |
2544 KB |
Output is correct |
23 |
Correct |
7 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
27 ms |
3672 KB |
Output is correct |
26 |
Correct |
20 ms |
3676 KB |
Output is correct |
27 |
Correct |
22 ms |
3904 KB |
Output is correct |
28 |
Correct |
22 ms |
3928 KB |
Output is correct |
29 |
Correct |
21 ms |
3932 KB |
Output is correct |
30 |
Correct |
22 ms |
3932 KB |
Output is correct |
31 |
Correct |
8 ms |
2392 KB |
Output is correct |
32 |
Correct |
7 ms |
2592 KB |
Output is correct |
33 |
Correct |
17 ms |
3420 KB |
Output is correct |
34 |
Correct |
8 ms |
2396 KB |
Output is correct |
35 |
Correct |
19 ms |
3676 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
19 ms |
3676 KB |
Output is correct |
39 |
Correct |
18 ms |
3752 KB |
Output is correct |
40 |
Correct |
22 ms |
3728 KB |
Output is correct |
41 |
Correct |
19 ms |
3676 KB |
Output is correct |
42 |
Correct |
19 ms |
3932 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
19 ms |
3932 KB |
Output is correct |
50 |
Correct |
19 ms |
3676 KB |
Output is correct |
51 |
Correct |
19 ms |
3676 KB |
Output is correct |
52 |
Correct |
20 ms |
3676 KB |
Output is correct |
53 |
Correct |
22 ms |
3728 KB |
Output is correct |
54 |
Correct |
24 ms |
3676 KB |
Output is correct |
55 |
Correct |
27 ms |
4188 KB |
Output is correct |
56 |
Correct |
8 ms |
2396 KB |
Output is correct |
57 |
Correct |
8 ms |
2396 KB |
Output is correct |
58 |
Correct |
74 ms |
3512 KB |
Output is correct |
59 |
Correct |
92 ms |
3636 KB |
Output is correct |
60 |
Correct |
125 ms |
3420 KB |
Output is correct |
61 |
Correct |
72 ms |
3416 KB |
Output is correct |
62 |
Correct |
82 ms |
3420 KB |
Output is correct |
63 |
Correct |
115 ms |
3620 KB |
Output is correct |
64 |
Correct |
543 ms |
4284 KB |
Output is correct |
65 |
Correct |
582 ms |
4148 KB |
Output is correct |
66 |
Correct |
767 ms |
4416 KB |
Output is correct |
67 |
Correct |
10 ms |
4312 KB |
Output is correct |
68 |
Correct |
21 ms |
4956 KB |
Output is correct |
69 |
Correct |
20 ms |
4956 KB |
Output is correct |
70 |
Correct |
23 ms |
4956 KB |
Output is correct |
71 |
Correct |
23 ms |
4916 KB |
Output is correct |
72 |
Correct |
21 ms |
4956 KB |
Output is correct |
73 |
Correct |
22 ms |
4952 KB |
Output is correct |
74 |
Correct |
23 ms |
4956 KB |
Output is correct |