Submission #6858958


Source Code Expand

#include <bits/stdc++.h>

using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

// end template


Graph makeReversed(const Graph &g) {
    int n = g.size();
    Graph revg(n);
    for (auto &es : g) {
        for (auto &e: es) {
            Edge re = e;
            std::swap(re.src, re.dst);
            revg[re.src].push_back(re);
        }
    }
    return revg;
}

Weight bidirectionalDijkstra(const Graph &g, int s, int t) {
    if (s == t) return 0;
    constexpr Weight INF = std::numeric_limits<Weight>::max() / 2;
    int n = g.size();
    using State = std::pair<Weight, int>;

    std::vector<Weight> dist[2];
    dist[0] = dist[1] = std::vector<Weight>(n, INF);
    dist[0][s] = dist[1][t] = 0;

    const Graph revg = makeReversed(g);
    const Graph* gs[2] = { &g, &revg };

    std::priority_queue<State, std::vector<State>, std::greater<State>> pq[2];
    pq[0].emplace(0, s);
    pq[1].emplace(0, t);

    Weight mu = INF;
    while (pq[0].size() && pq[1].size()) {
        if (pq[0].top().first + pq[1].top().first >= mu) break;
        int k = pq[0].top().first <= pq[1].top().first ? 0 : 1;
        Weight d;
        int v;
        std::tie(d, v) = pq[k].top();
        pq[k].pop();
        if (d < dist[k][v]) continue;
        for (auto &e : (*gs[k])[v]) {
            Weight w = e.dst;
            if (dist[k][w] > dist[k][v] + e.weight) {
                dist[k][w] = dist[k][v] + e.weight;
                pq[k].emplace(dist[k][w], w);
                mu = std::min(mu, dist[k][v] + e.weight + dist[1 - k][w]);
            }
        }
    }
    return mu;
}

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    Graph g(n);
    rep(i, m){
        int a, b, t;
        cin >> a >> b >> t;
        --a; --b;
        add_edge(g, a, b, t);
    }
    int res = 1e9;
    rep(i, n){
        int cand = 0;
        rep(j, n){
            cand = max(cand, bidirectionalDijkstra(g, i, j));
            if(cand > res) break;
        }
        res = min(res, cand);
    }
    cout << res << endl;
}

Submission Info

Submission Time
Task D - バスと避けられない運命
User tubo28
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2731 Byte
Status TLE
Exec Time 5255 ms
Memory 6088 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 35
TLE × 4
Set Name Test Cases
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 6 ms 384 KB
sample_03.txt AC 1 ms 256 KB
test_01.txt AC 1 ms 256 KB
test_02.txt AC 134 ms 256 KB
test_03.txt TLE 5255 ms 6088 KB
test_04.txt AC 949 ms 512 KB
test_05.txt TLE 5255 ms 2060 KB
test_06.txt AC 387 ms 716 KB
test_07.txt TLE 5255 ms 5052 KB
test_08.txt AC 8 ms 256 KB
test_09.txt AC 4332 ms 1860 KB
test_10.txt AC 106 ms 684 KB
test_11.txt AC 350 ms 712 KB
test_12.txt AC 3176 ms 876 KB
test_13.txt AC 311 ms 724 KB
test_14.txt AC 872 ms 788 KB
test_15.txt TLE 5255 ms 4884 KB
test_16.txt AC 572 ms 820 KB
test_17.txt AC 2611 ms 1932 KB
test_18.txt AC 2341 ms 860 KB
test_19.txt AC 893 ms 820 KB
test_20.txt AC 482 ms 796 KB
test_21.txt AC 1 ms 256 KB
test_22.txt AC 950 ms 728 KB
test_23.txt AC 4 ms 256 KB
test_24.txt AC 181 ms 256 KB
test_25.txt AC 1306 ms 256 KB
test_26.txt AC 52 ms 256 KB
test_27.txt AC 365 ms 256 KB
test_28.txt AC 106 ms 256 KB
test_29.txt AC 1252 ms 256 KB
test_30.txt AC 24 ms 256 KB
test_31.txt AC 4 ms 256 KB
test_32.txt AC 205 ms 256 KB
test_33.txt AC 1168 ms 256 KB
test_34.txt AC 5 ms 256 KB
test_35.txt AC 218 ms 256 KB
test_36.txt AC 138 ms 256 KB