Submission #757192

# Submission time Handle Problem Language Result Execution time Memory
757192 2023-06-12T18:59:11 Z sysia Olympic Bus (JOI20_ho_t4) C++17
37 / 100
1000 ms 5980 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

set<T>cycle;

vector<int> dijkstra(int n, vector<vector<T>>&g, int from, int to = -1, bool f = 0){
    vector<int>dist(n+1, oo);
    dist[from] = 0;
    set<T>s;
    vector<int>pre(n+1);
    s.insert({0, from});
    while ((int)s.size()){
        auto [d, v] = *s.begin();
        s.erase(s.begin());
        if (dist[v] < d) continue;
        for (auto [x, c]: g[v]){
            if (dist[x] > dist[v] + c){
                pre[x] = v;
                dist[x] = dist[v] + c;
                s.insert({dist[x], x});
            }
        }
    }
    if (f){
        int now = to;
        vector<int>path;
        while (now){
            path.emplace_back(now);
            now = pre[now];
        }
        reverse(path.begin(), path.end());
        for (int i = 1; i<(int)path.size(); i++) cycle.insert({path[i-1], path[i]});
    }
    return dist;
}

void solve(){
    int n, m; cin >> n >> m;
    vector<vector<T>>g(n+1), gt(n+1);
    vector<tuple<int, int, int, int>>edges;
    for (int i = 0; i<m; i++){
        int a, b, c, d; cin >> a >> b >> c >> d;
        g[a].emplace_back(b, c);
        gt[b].emplace_back(a, c);
        edges.emplace_back(a, b, c, d);
    }
    auto d1 = dijkstra(n, g, 1, n, 1); //1 do x
    auto dn = dijkstra(n, g, n, 1, 1); //n do x
    auto d1rev = dijkstra(n, gt, 1); //x do 1
    auto dnrev = dijkstra(n, gt, n); //x do n
    int ans = d1[n] + dn[1];
    int k = 0;
    debug(cycle);
    for (auto [a, b, c, d]: edges){
        if (cycle.find({a, b}) != cycle.end()){
            debug(a, b);
            int tmp = d;
            vector<vector<T>>G(n+1);
            for (int i = 0; i<m; i++){
                auto [A, B, C, D] = edges[i];
                if (i == k) {
                    G[B].emplace_back(A, C);
                    continue;
                }
                G[A].emplace_back(B, C);
            }
            tmp += dijkstra(n, G, 1)[n] + dijkstra(n, G, n)[1];
            ans = min(ans, tmp);
        } else {
            ans = min(ans, d + min(d1[n], d1[b]+c+dnrev[a]) + min(dn[1], dn[b] + c + d1rev[a]));
        }
        k++;
    }
    if (ans >= oo) cout << "-1\n";
    else cout << ans << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 12 ms 340 KB Output is correct
10 Correct 40 ms 468 KB Output is correct
11 Correct 48 ms 468 KB Output is correct
12 Correct 50 ms 468 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 5636 KB Output is correct
2 Correct 46 ms 5604 KB Output is correct
3 Correct 50 ms 5632 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 20 ms 4376 KB Output is correct
10 Correct 22 ms 4344 KB Output is correct
11 Correct 77 ms 5664 KB Output is correct
12 Correct 55 ms 5564 KB Output is correct
13 Correct 58 ms 5640 KB Output is correct
14 Correct 64 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 21 ms 4112 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 26 ms 5440 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 19 ms 4420 KB Output is correct
9 Correct 18 ms 4364 KB Output is correct
10 Correct 27 ms 5444 KB Output is correct
11 Correct 21 ms 5536 KB Output is correct
12 Correct 25 ms 5720 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 1 ms 212 KB Output is correct
19 Correct 25 ms 5828 KB Output is correct
20 Correct 33 ms 5456 KB Output is correct
21 Correct 26 ms 5572 KB Output is correct
22 Correct 26 ms 5436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 12 ms 340 KB Output is correct
10 Correct 40 ms 468 KB Output is correct
11 Correct 48 ms 468 KB Output is correct
12 Correct 50 ms 468 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 59 ms 5636 KB Output is correct
18 Correct 46 ms 5604 KB Output is correct
19 Correct 50 ms 5632 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 20 ms 4376 KB Output is correct
26 Correct 22 ms 4344 KB Output is correct
27 Correct 77 ms 5664 KB Output is correct
28 Correct 55 ms 5564 KB Output is correct
29 Correct 58 ms 5640 KB Output is correct
30 Correct 64 ms 5980 KB Output is correct
31 Correct 3 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 21 ms 4112 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 26 ms 5440 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 19 ms 4420 KB Output is correct
39 Correct 18 ms 4364 KB Output is correct
40 Correct 27 ms 5444 KB Output is correct
41 Correct 21 ms 5536 KB Output is correct
42 Correct 25 ms 5720 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 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 1 ms 212 KB Output is correct
49 Correct 25 ms 5828 KB Output is correct
50 Correct 33 ms 5456 KB Output is correct
51 Correct 26 ms 5572 KB Output is correct
52 Correct 26 ms 5436 KB Output is correct
53 Correct 37 ms 5432 KB Output is correct
54 Correct 54 ms 5552 KB Output is correct
55 Correct 50 ms 5444 KB Output is correct
56 Correct 3 ms 340 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 457 ms 4244 KB Output is correct
59 Correct 539 ms 4424 KB Output is correct
60 Correct 755 ms 4420 KB Output is correct
61 Correct 432 ms 4376 KB Output is correct
62 Correct 497 ms 4400 KB Output is correct
63 Correct 739 ms 4396 KB Output is correct
64 Execution timed out 1079 ms 5108 KB Time limit exceeded
65 Halted 0 ms 0 KB -