# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
705694 |
2023-03-05T01:19:21 Z |
cig32 |
Olympic Bus (JOI20_ho_t4) |
C++17 |
|
1000 ms |
7220 KB |
#include "bits/stdc++.h"
#define int long long
using namespace std;
struct edge {
int u, v, c, d;
};
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
edge e[m+1];
for(int i=1; i<=m; i++) {
cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
}
int dist1[n+1][n+1], distn[n+1][n+1];
int rev1[n+1][n+1], revn[n+1][n+1]; // b=0: nothing
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
dist1[i][j] = distn[i][j] = rev1[i][j] = revn[i][j] = 1e18;
}
dist1[i][1] = 0;
distn[i][n] = 0;
rev1[i][1] = 0;
revn[i][n] = 0;
}
for(int b=0; b<=n; b++) {
vector<pair<int, int> > adj[n+1], rev[n+1];
bool in[n+1];
queue<int> q;
for(int i=1; i<=m; i++) {
if(e[i].u != b && e[i].v != b) {
adj[e[i].u].push_back({e[i].v, e[i].c});
rev[e[i].v].push_back({e[i].u, e[i].c});
}
}
for(int i=1; i<=n; i++) in[i] = 0;
in[1] = 1;
q.push(1);
while(q.size()) {
int f = q.front();
q.pop();
in[f] = 0;
for(pair<int, int> x: adj[f]) {
if(dist1[b][x.first] > dist1[b][f] + x.second) {
dist1[b][x.first] = dist1[b][f] + x.second;
if(!in[x.first]) {
in[x.first] = 1;
q.push(x.first);
}
}
}
}
in[n] = 1;
q.push(n);
while(q.size()) {
int f = q.front();
q.pop();
in[f] = 0;
for(pair<int, int> x: adj[f]) {
if(distn[b][x.first] > distn[b][f] + x.second) {
distn[b][x.first] = distn[b][f] + x.second;
if(!in[x.first]) {
in[x.first] = 1;
q.push(x.first);
}
}
}
}
in[1] = 1;
q.push(1);
while(q.size()) {
int f = q.front();
q.pop();
in[f] = 0;
for(pair<int, int> x: rev[f]) {
if(rev1[b][x.first] > rev1[b][f] + x.second) {
rev1[b][x.first] = rev1[b][f] + x.second;
if(!in[x.first]) {
in[x.first] = 1;
q.push(x.first);
}
}
}
}
in[n] = 1;
q.push(n);
while(q.size()) {
int f = q.front();
q.pop();
in[f] = 0;
for(pair<int, int> x: rev[f]) {
if(revn[b][x.first] > revn[b][f] + x.second) {
revn[b][x.first] = revn[b][f] + x.second;
if(!in[x.first]) {
in[x.first] = 1;
q.push(x.first);
}
}
}
}
}
int ans = dist1[0][n] + distn[0][1];
vector<pair<int,int> > adj[n+1], rev[n+1];
vector<int> adj_id[n+1];
for(int i=1; i<=m; i++) {
adj_id[e[i].u].push_back(i);
adj[e[i].u].push_back({e[i].v, e[i].c});
rev[e[i].v].push_back({e[i].u, e[i].c});
}
int cnt[n+1][n+1];
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
cnt[i][j] = 0;
}
}
for(int i=1; i<=m; i++) {
cnt[e[i].u][e[i].v]++;
}
int opt1[n+1], opt2[n+1], opt3[n+1], opt4[n+1];
for(int i=1; i<=n; i++) {
opt1[i] = opt2[i] = opt3[i] = opt4[i] = 1e18;
for(pair<int, int> x: adj[i]) {
opt1[i] = min(opt1[i], x.second + revn[i][x.first]);
opt3[i] = min(opt3[i], x.second + rev1[i][x.first]);
}
for(pair<int, int> x: rev[i]) {
opt2[i] = min(opt2[i], dist1[i][x.first] + x.second);
opt4[i] = min(opt4[i], distn[i][x.first] + x.second);
}
}
for(int i=1; i<=m; i++) {
int forward, backward;
forward = dist1[e[i].u][e[i].v] + e[i].c;
int mi = (e[i].u == n ? 0 : 1e18);
mi = min(mi, opt1[e[i].u]);
forward += mi;
forward = min(forward, dist1[e[i].u][n]);
int pt1 = (e[i].u == 1 ? 0 : 1e18);
int pt2 = (e[i].u == n ? 0 : 1e18);
pt1 = min(pt1, opt2[e[i].u]);
int it=0;
for(pair<int,int> x: adj[e[i].u]) {
if(adj_id[e[i].u][it] == i) {
it++;
continue;
}
it++;
pt2 = min(pt2, revn[e[i].u][x.first] + x.second);
}
forward = min(forward, pt1 + pt2);
backward = distn[e[i].u][e[i].v] + e[i].c;
mi = (e[i].u == 1 ? 0 : 1e18);
mi = min(mi, opt3[e[i].u]);
backward += mi;
backward = min(backward, distn[e[i].u][1]);
pt1 = (e[i].u == n ? 0 : 1e18);
pt2 = (e[i].u == 1 ? 0 : 1e18);
pt1 = min(pt1, opt4[e[i].u]);
it=0;
for(pair<int,int> x: adj[e[i].u]) {
if(adj_id[e[i].u][it] == i) {
it++;
continue;
}
it++;
pt2 = min(pt2, rev1[e[i].u][x.first] + x.second);
}
backward = min(backward, pt1 + pt2);
ans = min(ans, forward + backward + e[i].d);
//cout << "flip edge " << i << ": " << forward << " + " << backward << " + " << e[i].d << " = " << forward + backward + e[i].d << "\n";
}
cout << (ans <= 1e12 ? ans : -1) << "\n";
}
/*
5 6
1 2 1 10000
2 5 1 10000
5 3 1 10000
2 3 10 1
2 4 1 10000
4 1 1 10000
2 2
1 2 3 4
1 2 3 9
3 4
1 2 3 4
2 3 3 9
2 1 3 10
2 3 3 11
2 7
1 2 1 1
1 2 1 7
1 2 1 3
1 2 1 4
1 2 1 5
1 2 1 6
1 2 1 9
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2004 KB |
Output is correct |
2 |
Correct |
5 ms |
1876 KB |
Output is correct |
3 |
Correct |
22 ms |
1952 KB |
Output is correct |
4 |
Correct |
24 ms |
1932 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
5 ms |
1876 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
47 ms |
1920 KB |
Output is correct |
11 |
Correct |
51 ms |
1940 KB |
Output is correct |
12 |
Correct |
49 ms |
1936 KB |
Output is correct |
13 |
Correct |
19 ms |
1940 KB |
Output is correct |
14 |
Correct |
21 ms |
1916 KB |
Output is correct |
15 |
Correct |
20 ms |
2004 KB |
Output is correct |
16 |
Correct |
22 ms |
1952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
610 ms |
6660 KB |
Output is correct |
2 |
Correct |
596 ms |
6812 KB |
Output is correct |
3 |
Correct |
630 ms |
6684 KB |
Output is correct |
4 |
Correct |
41 ms |
1996 KB |
Output is correct |
5 |
Correct |
19 ms |
2004 KB |
Output is correct |
6 |
Correct |
10 ms |
1892 KB |
Output is correct |
7 |
Correct |
7 ms |
1876 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
482 ms |
6724 KB |
Output is correct |
10 |
Correct |
493 ms |
6976 KB |
Output is correct |
11 |
Correct |
607 ms |
6852 KB |
Output is correct |
12 |
Correct |
626 ms |
6704 KB |
Output is correct |
13 |
Correct |
594 ms |
6792 KB |
Output is correct |
14 |
Correct |
609 ms |
7220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1996 KB |
Output is correct |
2 |
Correct |
6 ms |
1876 KB |
Output is correct |
3 |
Correct |
372 ms |
5428 KB |
Output is correct |
4 |
Correct |
6 ms |
1876 KB |
Output is correct |
5 |
Correct |
495 ms |
6564 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
485 ms |
6804 KB |
Output is correct |
9 |
Correct |
446 ms |
6612 KB |
Output is correct |
10 |
Correct |
490 ms |
6696 KB |
Output is correct |
11 |
Correct |
496 ms |
6716 KB |
Output is correct |
12 |
Correct |
495 ms |
6836 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
498 ms |
6848 KB |
Output is correct |
20 |
Correct |
504 ms |
6708 KB |
Output is correct |
21 |
Correct |
510 ms |
6776 KB |
Output is correct |
22 |
Correct |
492 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2004 KB |
Output is correct |
2 |
Correct |
5 ms |
1876 KB |
Output is correct |
3 |
Correct |
22 ms |
1952 KB |
Output is correct |
4 |
Correct |
24 ms |
1932 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
5 ms |
1876 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
47 ms |
1920 KB |
Output is correct |
11 |
Correct |
51 ms |
1940 KB |
Output is correct |
12 |
Correct |
49 ms |
1936 KB |
Output is correct |
13 |
Correct |
19 ms |
1940 KB |
Output is correct |
14 |
Correct |
21 ms |
1916 KB |
Output is correct |
15 |
Correct |
20 ms |
2004 KB |
Output is correct |
16 |
Correct |
22 ms |
1952 KB |
Output is correct |
17 |
Correct |
610 ms |
6660 KB |
Output is correct |
18 |
Correct |
596 ms |
6812 KB |
Output is correct |
19 |
Correct |
630 ms |
6684 KB |
Output is correct |
20 |
Correct |
41 ms |
1996 KB |
Output is correct |
21 |
Correct |
19 ms |
2004 KB |
Output is correct |
22 |
Correct |
10 ms |
1892 KB |
Output is correct |
23 |
Correct |
7 ms |
1876 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
482 ms |
6724 KB |
Output is correct |
26 |
Correct |
493 ms |
6976 KB |
Output is correct |
27 |
Correct |
607 ms |
6852 KB |
Output is correct |
28 |
Correct |
626 ms |
6704 KB |
Output is correct |
29 |
Correct |
594 ms |
6792 KB |
Output is correct |
30 |
Correct |
609 ms |
7220 KB |
Output is correct |
31 |
Correct |
20 ms |
1996 KB |
Output is correct |
32 |
Correct |
6 ms |
1876 KB |
Output is correct |
33 |
Correct |
372 ms |
5428 KB |
Output is correct |
34 |
Correct |
6 ms |
1876 KB |
Output is correct |
35 |
Correct |
495 ms |
6564 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
485 ms |
6804 KB |
Output is correct |
39 |
Correct |
446 ms |
6612 KB |
Output is correct |
40 |
Correct |
490 ms |
6696 KB |
Output is correct |
41 |
Correct |
496 ms |
6716 KB |
Output is correct |
42 |
Correct |
495 ms |
6836 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
498 ms |
6848 KB |
Output is correct |
50 |
Correct |
504 ms |
6708 KB |
Output is correct |
51 |
Correct |
510 ms |
6776 KB |
Output is correct |
52 |
Correct |
492 ms |
6740 KB |
Output is correct |
53 |
Correct |
617 ms |
6532 KB |
Output is correct |
54 |
Correct |
618 ms |
6616 KB |
Output is correct |
55 |
Correct |
614 ms |
6672 KB |
Output is correct |
56 |
Correct |
23 ms |
2004 KB |
Output is correct |
57 |
Correct |
24 ms |
1936 KB |
Output is correct |
58 |
Correct |
666 ms |
5388 KB |
Output is correct |
59 |
Correct |
724 ms |
5380 KB |
Output is correct |
60 |
Correct |
775 ms |
5356 KB |
Output is correct |
61 |
Correct |
642 ms |
5488 KB |
Output is correct |
62 |
Correct |
669 ms |
5508 KB |
Output is correct |
63 |
Correct |
692 ms |
5360 KB |
Output is correct |
64 |
Execution timed out |
1076 ms |
5260 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |