#include <bits/stdc++.h>
using namespace std;
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = ll(4e18) + ll(2e15);
const double EPS = 1e-9;
static int LamyIsCute = []() {
EmiliaMyWife
return 48763;
}();
const int N = 207, M = 5e4 + 25;
struct dat {
int v, c, i;
};
vector<dat> edge[N], redge[N];
bool is[M];
int n;
int len[M], cost[M];
ll dis[N][N];
pair<int, int> edges[M];
ll dijk(int s, int t, int id) {
vector<ll> dis(n + 1, LINF);
vector<bool> vis(n + 1);
dis[s] = 0;
while(true) {
int u = -1;
for(int i = 1; i <= n; i++)
if(!vis[i] && (u == -1 || dis[u] > dis[i]))
u = i;
if(u == -1)
break;
vis[u] = true;
for(const auto &[v, c, i] : edge[u])
if(i != id && dis[v] > dis[u] + c)
dis[v] = dis[u] + c;
}
return dis[t];
}
ll brute(int id) {
if(~id)
edge[edges[id].second].push_back({edges[id].first, len[id], -1});
ll ret = dijk(1, n, id) + dijk(n, 1, id);
if(~id)
edge[edges[id].second].pop_back();
return ret;
}
void dijk2(int s) {
vector<ll> dis(n + 1, LINF);
vector<int> lst(n + 1, -1);
dis[s] = 0;
vector<bool> vis(n + 1);
while(true) {
int u = -1;
for(int i = 1; i <= n; i++)
if(!vis[i] && (u == -1 || dis[u] > dis[i]))
u = i;
if(u == -1 || dis[u] == LINF)
break;
if(~lst[u])
is[lst[u]] = true;
vis[u] = true;
for(const auto &[v, c, id] : edge[u])
if(dis[v] > dis[u] + c)
dis[v] = dis[u] + c, lst[v] = id;
}
}
signed main() {
int m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
edge[u].push_back({v, c, i});
redge[v].push_back({u, c, i});
cost[i] = d;
len[i] = c;
edges[i] = {u, v};
}
dijk2(1);
dijk2(n);
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if(i != j)
dis[i][j] = LINF;
for(int i = 0; i < m; i++)
dis[edges[i].first][edges[i].second] = min<ll>(dis[edges[i].first][edges[i].second], len[i]);
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
ll ans = dis[1][n] + dis[n][1];
for(int i = 0; i < m; i++) {
const auto &[u, v] = edges[i];
if(is[i])
ans = min(ans, brute(i) + cost[i]);
else
ans = min(ans, min(dis[1][n], dis[1][v] + dis[u][n] + len[i]) + min(dis[n][1], dis[n][v] + dis[u][1] + len[i]) + cost[i]);
}
if(ans > 2e9)
cout << "-1\n";
else
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
716 KB |
Output is correct |
2 |
Correct |
12 ms |
688 KB |
Output is correct |
3 |
Correct |
58 ms |
732 KB |
Output is correct |
4 |
Correct |
60 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
688 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
46 ms |
720 KB |
Output is correct |
11 |
Correct |
61 ms |
724 KB |
Output is correct |
12 |
Correct |
55 ms |
720 KB |
Output is correct |
13 |
Correct |
32 ms |
716 KB |
Output is correct |
14 |
Correct |
50 ms |
716 KB |
Output is correct |
15 |
Correct |
48 ms |
716 KB |
Output is correct |
16 |
Correct |
44 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
3452 KB |
Output is correct |
2 |
Correct |
119 ms |
3468 KB |
Output is correct |
3 |
Correct |
107 ms |
3404 KB |
Output is correct |
4 |
Correct |
53 ms |
792 KB |
Output is correct |
5 |
Correct |
46 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
588 KB |
Output is correct |
7 |
Correct |
14 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
78 ms |
3412 KB |
Output is correct |
10 |
Correct |
65 ms |
3408 KB |
Output is correct |
11 |
Correct |
93 ms |
3508 KB |
Output is correct |
12 |
Correct |
95 ms |
3492 KB |
Output is correct |
13 |
Correct |
95 ms |
3584 KB |
Output is correct |
14 |
Correct |
92 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
716 KB |
Output is correct |
2 |
Correct |
24 ms |
588 KB |
Output is correct |
3 |
Correct |
112 ms |
2688 KB |
Output is correct |
4 |
Correct |
15 ms |
588 KB |
Output is correct |
5 |
Correct |
122 ms |
3420 KB |
Output is correct |
6 |
Correct |
0 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
75 ms |
3404 KB |
Output is correct |
9 |
Correct |
78 ms |
3408 KB |
Output is correct |
10 |
Correct |
107 ms |
3404 KB |
Output is correct |
11 |
Correct |
96 ms |
3404 KB |
Output is correct |
12 |
Correct |
101 ms |
3512 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
0 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
588 KB |
Output is correct |
19 |
Correct |
102 ms |
3552 KB |
Output is correct |
20 |
Correct |
97 ms |
3448 KB |
Output is correct |
21 |
Correct |
100 ms |
3336 KB |
Output is correct |
22 |
Correct |
100 ms |
3432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
716 KB |
Output is correct |
2 |
Correct |
12 ms |
688 KB |
Output is correct |
3 |
Correct |
58 ms |
732 KB |
Output is correct |
4 |
Correct |
60 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
688 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
46 ms |
720 KB |
Output is correct |
11 |
Correct |
61 ms |
724 KB |
Output is correct |
12 |
Correct |
55 ms |
720 KB |
Output is correct |
13 |
Correct |
32 ms |
716 KB |
Output is correct |
14 |
Correct |
50 ms |
716 KB |
Output is correct |
15 |
Correct |
48 ms |
716 KB |
Output is correct |
16 |
Correct |
44 ms |
716 KB |
Output is correct |
17 |
Correct |
108 ms |
3452 KB |
Output is correct |
18 |
Correct |
119 ms |
3468 KB |
Output is correct |
19 |
Correct |
107 ms |
3404 KB |
Output is correct |
20 |
Correct |
53 ms |
792 KB |
Output is correct |
21 |
Correct |
46 ms |
716 KB |
Output is correct |
22 |
Correct |
11 ms |
588 KB |
Output is correct |
23 |
Correct |
14 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
78 ms |
3412 KB |
Output is correct |
26 |
Correct |
65 ms |
3408 KB |
Output is correct |
27 |
Correct |
93 ms |
3508 KB |
Output is correct |
28 |
Correct |
95 ms |
3492 KB |
Output is correct |
29 |
Correct |
95 ms |
3584 KB |
Output is correct |
30 |
Correct |
92 ms |
3704 KB |
Output is correct |
31 |
Correct |
47 ms |
716 KB |
Output is correct |
32 |
Correct |
24 ms |
588 KB |
Output is correct |
33 |
Correct |
112 ms |
2688 KB |
Output is correct |
34 |
Correct |
15 ms |
588 KB |
Output is correct |
35 |
Correct |
122 ms |
3420 KB |
Output is correct |
36 |
Correct |
0 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
588 KB |
Output is correct |
38 |
Correct |
75 ms |
3404 KB |
Output is correct |
39 |
Correct |
78 ms |
3408 KB |
Output is correct |
40 |
Correct |
107 ms |
3404 KB |
Output is correct |
41 |
Correct |
96 ms |
3404 KB |
Output is correct |
42 |
Correct |
101 ms |
3512 KB |
Output is correct |
43 |
Correct |
1 ms |
588 KB |
Output is correct |
44 |
Correct |
1 ms |
588 KB |
Output is correct |
45 |
Correct |
1 ms |
588 KB |
Output is correct |
46 |
Correct |
1 ms |
588 KB |
Output is correct |
47 |
Correct |
0 ms |
588 KB |
Output is correct |
48 |
Correct |
0 ms |
588 KB |
Output is correct |
49 |
Correct |
102 ms |
3552 KB |
Output is correct |
50 |
Correct |
97 ms |
3448 KB |
Output is correct |
51 |
Correct |
100 ms |
3336 KB |
Output is correct |
52 |
Correct |
100 ms |
3432 KB |
Output is correct |
53 |
Correct |
100 ms |
3376 KB |
Output is correct |
54 |
Correct |
112 ms |
3412 KB |
Output is correct |
55 |
Correct |
105 ms |
3404 KB |
Output is correct |
56 |
Correct |
61 ms |
716 KB |
Output is correct |
57 |
Correct |
52 ms |
724 KB |
Output is correct |
58 |
Correct |
73 ms |
2632 KB |
Output is correct |
59 |
Correct |
86 ms |
2808 KB |
Output is correct |
60 |
Correct |
105 ms |
2636 KB |
Output is correct |
61 |
Correct |
76 ms |
2648 KB |
Output is correct |
62 |
Correct |
96 ms |
2624 KB |
Output is correct |
63 |
Correct |
109 ms |
2680 KB |
Output is correct |
64 |
Correct |
123 ms |
2912 KB |
Output is correct |
65 |
Correct |
129 ms |
2892 KB |
Output is correct |
66 |
Correct |
167 ms |
2932 KB |
Output is correct |
67 |
Correct |
12 ms |
2364 KB |
Output is correct |
68 |
Correct |
67 ms |
3392 KB |
Output is correct |
69 |
Correct |
65 ms |
3372 KB |
Output is correct |
70 |
Correct |
114 ms |
3420 KB |
Output is correct |
71 |
Correct |
105 ms |
3396 KB |
Output is correct |
72 |
Correct |
96 ms |
3404 KB |
Output is correct |
73 |
Correct |
96 ms |
3444 KB |
Output is correct |
74 |
Correct |
94 ms |
3484 KB |
Output is correct |