Submission #891459

# Submission time Handle Problem Language Result Execution time Memory
891459 2023-12-23T04:59:40 Z hafo Olympic Bus (JOI20_ho_t4) C++14
100 / 100
52 ms 3852 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
 
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
 
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 200 + 7;
const ll oo = 1e15 + 69;
const int maxe = 5e4 + 7;
 
int n, m;
pa trace[maxn][maxn];
ll c[maxn][maxn], c2[maxn][maxn], best_without_u[maxe];
struct edge {
    int u, v, c, d;
} ed[maxe];
bool used[maxe];
 
void solve(int s, int t) {
    vector<pa> path;
    fill_n(used, m + 1, 0);
 
    int p = s;
    while(p != t) {
        path.pb({p, trace[p][t].se});
        used[trace[p][t].se] = 1;
        p = trace[p][t].fi;
    }
    path.pb({t, 0});
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) c2[i][j] = (i == j ? 0:oo);
    }
    
    for(int i = 1; i <= m; i++) {
        if(used[i]) continue;
        int u = ed[i].u, v = ed[i].v;
        best_without_u[i] += min(c[s][v] + c[u][t] + ed[i].c, c[s][t]);
        mini(c2[ed[i].u][ed[i].v], ed[i].c);
    }
 
    for(int k = 1; k <= n; k++) {
        for(int u = 1; u <= n; u++) {
            for(int v = 1; v <= n; v++) mini(c2[u][v], c2[u][k] + c2[k][v]);
        }
    }
 
    for(int i = 0; i < Size(path) - 1; i++) {
        ll mn = oo;
        for(int j = 0; j <= i; j++) {
            for(int k = i + 1; k < Size(path); k++) {
                int u = path[j].fi;
                int v = path[k].fi;
                mini(mn, c[s][u] + c2[u][v] + c[v][t]);
            }
        }
        best_without_u[path[i].se] += mn;
    }
 
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);
 
    cin>>n>>m;
    for(int i = 1; i <= m; i++) {
        cin>>ed[i].u>>ed[i].v>>ed[i].c>>ed[i].d;
    }
 
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            c[i][j] = (i == j ? 0:oo);
            trace[i][j] = {j, 0};
        }
    }
 
    for(int i = 1; i <= m; i++) {
        if(mini(c[ed[i].u][ed[i].v], ed[i].c)) trace[ed[i].u][ed[i].v].se = i;
    }
    for(int k = 1; k <= n; k++) {
        for(int u = 1; u <= n; u++) {
            for(int v = 1; v <= n; v++) {
                if(mini(c[u][v], c[u][k] + c[k][v])) trace[u][v] = trace[u][k];
            }
        }
    }
 
    ll ans = oo;
    mini(ans, c[1][n] + c[n][1]);
    solve(1, n);
    solve(n, 1);

    for(int i = 1; i <= m; i++) {
        mini(ans, best_without_u[i] + ed[i].d);
    }
    cout<<(ans == oo ? -1:ans);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 2392 KB Output is correct
2 Correct 17 ms 2536 KB Output is correct
3 Correct 22 ms 2564 KB Output is correct
4 Correct 21 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 16 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 27 ms 2652 KB Output is correct
11 Correct 24 ms 2392 KB Output is correct
12 Correct 24 ms 2396 KB Output is correct
13 Correct 19 ms 2392 KB Output is correct
14 Correct 20 ms 2396 KB Output is correct
15 Correct 18 ms 2564 KB Output is correct
16 Correct 19 ms 2560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3676 KB Output is correct
2 Correct 36 ms 3420 KB Output is correct
3 Correct 38 ms 3676 KB Output is correct
4 Correct 21 ms 2392 KB Output is correct
5 Correct 19 ms 2392 KB Output is correct
6 Correct 17 ms 2540 KB Output is correct
7 Correct 17 ms 2536 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 52 ms 3852 KB Output is correct
10 Correct 34 ms 3672 KB Output is correct
11 Correct 36 ms 3672 KB Output is correct
12 Correct 33 ms 3676 KB Output is correct
13 Correct 40 ms 3676 KB Output is correct
14 Correct 32 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2392 KB Output is correct
2 Correct 18 ms 2392 KB Output is correct
3 Correct 26 ms 3416 KB Output is correct
4 Correct 16 ms 2396 KB Output is correct
5 Correct 41 ms 3668 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 28 ms 3568 KB Output is correct
9 Correct 28 ms 3840 KB Output is correct
10 Correct 27 ms 3416 KB Output is correct
11 Correct 27 ms 3420 KB Output is correct
12 Correct 27 ms 3672 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2516 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 0 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 32 ms 3416 KB Output is correct
20 Correct 28 ms 3420 KB Output is correct
21 Correct 27 ms 3420 KB Output is correct
22 Correct 32 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 2392 KB Output is correct
2 Correct 17 ms 2536 KB Output is correct
3 Correct 22 ms 2564 KB Output is correct
4 Correct 21 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 16 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 27 ms 2652 KB Output is correct
11 Correct 24 ms 2392 KB Output is correct
12 Correct 24 ms 2396 KB Output is correct
13 Correct 19 ms 2392 KB Output is correct
14 Correct 20 ms 2396 KB Output is correct
15 Correct 18 ms 2564 KB Output is correct
16 Correct 19 ms 2560 KB Output is correct
17 Correct 37 ms 3676 KB Output is correct
18 Correct 36 ms 3420 KB Output is correct
19 Correct 38 ms 3676 KB Output is correct
20 Correct 21 ms 2392 KB Output is correct
21 Correct 19 ms 2392 KB Output is correct
22 Correct 17 ms 2540 KB Output is correct
23 Correct 17 ms 2536 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 52 ms 3852 KB Output is correct
26 Correct 34 ms 3672 KB Output is correct
27 Correct 36 ms 3672 KB Output is correct
28 Correct 33 ms 3676 KB Output is correct
29 Correct 40 ms 3676 KB Output is correct
30 Correct 32 ms 3676 KB Output is correct
31 Correct 17 ms 2392 KB Output is correct
32 Correct 18 ms 2392 KB Output is correct
33 Correct 26 ms 3416 KB Output is correct
34 Correct 16 ms 2396 KB Output is correct
35 Correct 41 ms 3668 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 28 ms 3568 KB Output is correct
39 Correct 28 ms 3840 KB Output is correct
40 Correct 27 ms 3416 KB Output is correct
41 Correct 27 ms 3420 KB Output is correct
42 Correct 27 ms 3672 KB Output is correct
43 Correct 1 ms 2396 KB Output is correct
44 Correct 0 ms 2516 KB Output is correct
45 Correct 1 ms 2396 KB Output is correct
46 Correct 0 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 32 ms 3416 KB Output is correct
50 Correct 28 ms 3420 KB Output is correct
51 Correct 27 ms 3420 KB Output is correct
52 Correct 32 ms 3420 KB Output is correct
53 Correct 37 ms 3676 KB Output is correct
54 Correct 37 ms 3420 KB Output is correct
55 Correct 37 ms 3420 KB Output is correct
56 Correct 26 ms 2392 KB Output is correct
57 Correct 21 ms 2396 KB Output is correct
58 Correct 33 ms 3452 KB Output is correct
59 Correct 35 ms 3416 KB Output is correct
60 Correct 36 ms 3416 KB Output is correct
61 Correct 33 ms 3292 KB Output is correct
62 Correct 35 ms 3420 KB Output is correct
63 Correct 35 ms 3420 KB Output is correct
64 Correct 31 ms 3480 KB Output is correct
65 Correct 31 ms 3420 KB Output is correct
66 Correct 32 ms 3420 KB Output is correct
67 Correct 12 ms 3416 KB Output is correct
68 Correct 37 ms 3824 KB Output is correct
69 Correct 34 ms 3672 KB Output is correct
70 Correct 33 ms 3676 KB Output is correct
71 Correct 32 ms 3804 KB Output is correct
72 Correct 33 ms 3676 KB Output is correct
73 Correct 32 ms 3676 KB Output is correct
74 Correct 33 ms 3676 KB Output is correct