Submission #958423

# Submission time Handle Problem Language Result Execution time Memory
958423 2024-04-05T19:02:30 Z BhavayGoyal Olympic Bus (JOI20_ho_t4) C++14
0 / 100
1000 ms 7176 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const int linf = 1e15;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 205, M = 50005;
int n, m, dist[N][N];
set<pii> g[N];
vii edges, shortestEdges;
map<pair<int, int>, bool> isShort;
map<pii, pii> edge;

pair<vi, vi> dijkstra(int source) {
    priority_queue<pii, vector<pii>, greater<>> q;
    vi vis(n+1, 0), dis(n+1, linf), par(n+1);
    q.push({0, source});
    dis[source] = 0;
    while (!q.empty()) {
        pii f = q.top(); q.pop();
        int src = f.s;
        if (vis[src]) continue;
        vis[src] = true;

        for (auto child : g[src]) {
            int ch = child.f, wt = child.s;
            if (dis[ch] > dis[src] + wt) {
                par[ch] = src;
                dis[ch] = dis[src] + wt;
                q.push({dis[ch], ch});
            }
        }
    }
    return {dis, par};
}

void sol() {
    for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (i != j) dist[i][j] = linf;

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c, d; cin >> a >> b >> c >> d;
        dist[a][b] = c;
        edge[{a, b}] = {c, d};
        g[a].insert({b, c});
        edges.pb({a, b, c, d});
    }

    pair<vi, vi> x = dijkstra(1); vi dis = x.f, par = x.s;
    int c = n;
    while (par[c]) {
        pii temp = edge[{par[c], c}];
        shortestEdges.pb({par[c], c, temp.f, temp.s});
        isShort[{par[c], c}] = true;
        c = par[c];
    }

    x = dijkstra(n); dis = x.f, par = x.s;
    c = 1;
    while (par[c]) {
        pii temp = edge[{par[c], c}];
        shortestEdges.pb({par[c], c, temp.f, temp.s});
        isShort[{par[c], c}] = true;
        c = par[c];
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    int ans = dist[1][n] + dist[n][1];

    // for (auto i : edges) {
    //     int a = i[0], b = i[1], c = i[2], d = i[3];
    //     if (isShort[{a, b}]) continue;
    //     ans = min(ans, dist[1][n] + dist[n][b] + c + dist[a][1] + d);
    //     ans = min(ans, dist[n][1] + dist[1][b] + c + dist[a][n] + d);
    // }

    for (auto i : edges) {
        int a = i[0], b = i[1], c = i[2], d = i[3];
        g[a].erase({b, c});
        g[b].insert({a, c});
        pair<vi, vi> x = dijkstra(1);
        pair<vi, vi> y = dijkstra(n);
        ans = min(ans, x.f[n] + y.f[1] + d);
        g[b].erase({a, c});
        g[a].insert({b, c});
    }

    cout << (ans>=1e10?-1:ans) << endl;
}

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 1040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1042 ms 7176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 1040 KB Output isn't correct
2 Halted 0 ms 0 KB -