Submission #757662

# Submission time Handle Problem Language Result Execution time Memory
757662 2023-06-13T14:18:31 Z sysia Olympic Bus (JOI20_ho_t4) C++17
21 / 100
1000 ms 5612 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 ll long long
typedef pair<int, int> T;
set<T>cycle;
const int oo2 = 2e9+7;
const int mx = 202;
int n;
int pre[mx];
priority_queue<T>pq;
 
vector<int> dijkstra(vector<int>&dist, vector<vector<T>>&g, int from, int to = -1){
    dist[from] = 0;
    
    if (to != -1) {
        for (int i = 1; i<=n; i++) pre[i] = 0;
    }
    pq.push({0, from});
    while ((int)pq.size()){
        auto [d, v] = pq.top();
        pq.pop();
        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;
                pq.push({dist[x], x});
            }
        }
    }
    if (to != -1){
        int now = to;
        int prev = -1;
        while (now){
            if (prev != -1) cycle.insert({now, prev});
            prev = now;
            now = pre[now];
        }
    }
    return dist;
}
 
int dist[mx];
int dijkstra2(vector<multiset<T>>&g, int from, int to = -1){
    for (int i = 1; i<=n; i++) dist[i] = oo2;
    dist[from] = 0;
    pq.push({0, from});
    while ((int)pq.size()){
        auto [d, v] = pq.top(); pq.pop();
        if (dist[v] < d) continue;
        for (auto [x, c]: g[v]){
            if (dist[x] > dist[v] + c){
                dist[x] = dist[v] + c;
                pq.push({dist[x], x});
            }
        }
    }
    return dist[to];
}
 
void solve(){
    int 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);
    }
    vector<int>d1(n+1, oo2), dn(n+1, oo2), d1rev(n+1, oo2), dnrev(n+1, oo2);
    dijkstra(d1, g, 1, n); //1 do x
    dijkstra(dn, g, n, 1); //n do x
    dijkstra(d1rev, gt, 1); //x do 1
    dijkstra(dnrev, gt, n); //x do n
    long long ans = (long long)d1[n] + dn[1];
    int k = 0;
    debug(cycle);
    vector<multiset<T>>G(n+1);
    for (auto [a, b, c, d]: edges){
        G[a].insert({b, c});
    }
    for (auto &[a, b, c, d]: edges){
        if (cycle.find({a, b}) != cycle.end()){
            debug(a, b);
            auto [A, B, C, D] = edges[k];
            G[A].erase(G[A].find({B, C}));
            G[B].insert({A, C});
            for (int i = 1; i<=n; i++){
                debug(i, G[i]);
            }
            long long tmp = (long long)d + (long long)dijkstra2(G, 1, n) + (long long)dijkstra2(G, n, 1);
            debug(tmp);
            G[B].erase(G[B].find({A, C}));
            G[A].insert({B, C});
            ans = min(ans, tmp);
        } else {
            ans = min(ans, (ll)d + min((ll)d1[n], (ll)d1[b]+c+dnrev[a]) + min((ll)dn[1], (ll)dn[b] + c + d1rev[a]));
        }
        k++;
    }
    if (ans >= oo2) cout << "-1\n";
    else cout << ans << "\n";
}
 
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    solve();
 
    return 0;
}
# 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 340 KB Output is correct
4 Correct 39 ms 420 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 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 17 ms 408 KB Output is correct
10 Correct 723 ms 432 KB Output is correct
11 Execution timed out 1067 ms 404 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1060 ms 4512 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 33 ms 3520 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 48 ms 5440 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 30 ms 5520 KB Output is correct
9 Correct 30 ms 5568 KB Output is correct
10 Correct 44 ms 5468 KB Output is correct
11 Correct 36 ms 5464 KB Output is correct
12 Correct 42 ms 5504 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 54 ms 5612 KB Output is correct
20 Correct 49 ms 5440 KB Output is correct
21 Correct 40 ms 5392 KB Output is correct
22 Correct 39 ms 5432 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 340 KB Output is correct
4 Correct 39 ms 420 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 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 17 ms 408 KB Output is correct
10 Correct 723 ms 432 KB Output is correct
11 Execution timed out 1067 ms 404 KB Time limit exceeded
12 Halted 0 ms 0 KB -