Submission #203489

# Submission time Handle Problem Language Result Execution time Memory
203489 2020-02-20T22:56:17 Z tri Olympic Bus (JOI20_ho_t4) C++14
100 / 100
377 ms 2680 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second


const int MAXV = 205;
const int MAXE = 5e4 + 10;
const ll INF = 1e15;

int V, E;

int u[MAXE], v[MAXE];
ll c[MAXE], r[MAXE];

int src;
int sink;

int aM[MAXV][MAXV];

ll cost[MAXV];
bool vis[MAXV];

int pEdge[MAXV];

vi sEdges;

void dijkstra() {
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            aM[i][j] = -1;
        }
    }

    for (int i = 0; i < E; i++) {
        if (aM[u[i]][v[i]] == -1 || c[aM[u[i]][v[i]]] > c[i]) {
            aM[u[i]][v[i]] = i;
        }
    }

    fill(cost, cost + MAXV, INF);
    fill(vis, vis + MAXV, false);

    fill(pEdge, pEdge + MAXV, -1);
    sEdges.clear();

    cost[src] = 0;

    while (true) {
        int cV = -1;
        for (int i = 0; i < V; i++) {
            if (!vis[i]) {
                if (cV == -1 || cost[i] < cost[cV]) {
                    cV = i;
                }
            }
        }
        if (cV == -1) {
            break;
        }
        vis[cV] = true;

        ll cC = cost[cV];

        for (int aV = 0; aV < V; aV++) {
            int aE = aM[cV][aV];
            if (aE == -1) continue;

            if (cost[aV] > cC + c[aE]) {
                cost[aV] = cC + c[aE];
                pEdge[aV] = aE;
            }
        }
    }

    int cV = sink;
    while (cV != src) {
        if (pEdge[cV] == -1) break;
        sEdges.pb(pEdge[cV]);
        cV = u[pEdge[cV]];

        assert(sEdges.size() < V);
    }
}

bool specEdge[MAXE];

ll g1Cost[MAXV], g2Cost[MAXV];

ll partAns[MAXE];

// compute cost for all reversed edges from src to sink
void computeCost() {
    dijkstra();

//    ps("fin1");

    fill(specEdge, specEdge + MAXE, false);
    for (int x : sEdges) {
        specEdge[x] = true;
    }
    assert(sEdges.size() < V);

    memcpy(g1Cost, cost, sizeof(cost));

    for (int i = 0; i < E; i++) {
        int temp = u[i];
        u[i] = v[i];
        v[i] = temp;
    }
    swap(src, sink);

    dijkstra();
    memcpy(g2Cost, cost, sizeof(cost));
    assert(g1Cost[sink] == g2Cost[src]);

    for (int i = 0; i < E; i++) {
        int temp = u[i];
        u[i] = v[i];
        v[i] = temp;
    }
    swap(src, sink);

//    ps("fin2");

    fill(partAns, partAns + MAXE, INF);

    for (int i = 0; i < E; i++) {
        if (g1Cost[v[i]] < g1Cost[u[i]]) { // edge is against gradient
            partAns[i] = g1Cost[v[i]] + g2Cost[u[i]] + c[i];
            partAns[i] = min(partAns[i], g1Cost[sink]); // don't use reverse edge
        } else {
            if (specEdge[i]) {
                swap(u[i], v[i]);
                dijkstra();
                swap(u[i], v[i]);

                partAns[i] = cost[sink];
            } else {
                partAns[i] = g1Cost[sink]; // just use the already found shortest path
            }
        }
    }
}

ll ans[MAXE];

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

    cin >> V >> E;

    for (int i = 0; i < E; i++) {
        cin >> u[i] >> v[i] >> c[i] >> r[i];
        u[i]--, v[i]--;
    }

    src = 0;
    sink = V - 1;

    computeCost();
    for (int i = 0; i < E; i++) {
        ans[i] = partAns[i] + r[i];
    }
    ans[E] = g1Cost[sink];

//    ps("half");
//    ps(partAns[1]);

    swap(src, sink);

    computeCost();
    for (int i = 0; i < E; i++) {
        ans[i] += partAns[i];
    }
    ans[E] += g1Cost[sink];
//
//    ps("full");
//    ps(partAns[1]);

    ll fAns = INF;
    for (int i = 0; i <= E; i++) {
        fAns = min(fAns, ans[i]);
    }

    assert(fAns >= 0);

    if (fAns == INF) {
        fAns = -1;
    }
    cout << fAns << endl;
}

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from ho_t4.cpp:1:
ho_t4.cpp: In function 'void dijkstra()':
ho_t4.cpp:94:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         assert(sEdges.size() < V);
                ~~~~~~~~~~~~~~^~~
ho_t4.cpp: In function 'void computeCost()':
ho_t4.cpp:114:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     assert(sEdges.size() < V);
            ~~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1016 KB Output is correct
2 Correct 6 ms 1016 KB Output is correct
3 Correct 8 ms 1016 KB Output is correct
4 Correct 9 ms 1016 KB Output is correct
5 Correct 6 ms 888 KB Output is correct
6 Correct 6 ms 1016 KB Output is correct
7 Correct 5 ms 760 KB Output is correct
8 Correct 5 ms 760 KB Output is correct
9 Correct 5 ms 888 KB Output is correct
10 Correct 72 ms 1016 KB Output is correct
11 Correct 70 ms 1016 KB Output is correct
12 Correct 77 ms 1144 KB Output is correct
13 Correct 6 ms 1016 KB Output is correct
14 Correct 8 ms 1016 KB Output is correct
15 Correct 8 ms 1016 KB Output is correct
16 Correct 8 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 2552 KB Output is correct
2 Correct 34 ms 2572 KB Output is correct
3 Correct 35 ms 2552 KB Output is correct
4 Correct 10 ms 1016 KB Output is correct
5 Correct 9 ms 1016 KB Output is correct
6 Correct 6 ms 1016 KB Output is correct
7 Correct 6 ms 1016 KB Output is correct
8 Correct 5 ms 760 KB Output is correct
9 Correct 29 ms 2552 KB Output is correct
10 Correct 30 ms 2552 KB Output is correct
11 Correct 39 ms 2552 KB Output is correct
12 Correct 35 ms 2552 KB Output is correct
13 Correct 35 ms 2552 KB Output is correct
14 Correct 38 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1016 KB Output is correct
2 Correct 6 ms 1016 KB Output is correct
3 Correct 33 ms 2168 KB Output is correct
4 Correct 6 ms 1016 KB Output is correct
5 Correct 29 ms 2680 KB Output is correct
6 Correct 5 ms 760 KB Output is correct
7 Correct 5 ms 888 KB Output is correct
8 Correct 28 ms 2552 KB Output is correct
9 Correct 27 ms 2552 KB Output is correct
10 Correct 29 ms 2552 KB Output is correct
11 Correct 28 ms 2552 KB Output is correct
12 Correct 28 ms 2552 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 5 ms 760 KB Output is correct
15 Correct 5 ms 760 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 5 ms 760 KB Output is correct
18 Correct 5 ms 760 KB Output is correct
19 Correct 27 ms 2552 KB Output is correct
20 Correct 29 ms 2552 KB Output is correct
21 Correct 28 ms 2552 KB Output is correct
22 Correct 28 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1016 KB Output is correct
2 Correct 6 ms 1016 KB Output is correct
3 Correct 8 ms 1016 KB Output is correct
4 Correct 9 ms 1016 KB Output is correct
5 Correct 6 ms 888 KB Output is correct
6 Correct 6 ms 1016 KB Output is correct
7 Correct 5 ms 760 KB Output is correct
8 Correct 5 ms 760 KB Output is correct
9 Correct 5 ms 888 KB Output is correct
10 Correct 72 ms 1016 KB Output is correct
11 Correct 70 ms 1016 KB Output is correct
12 Correct 77 ms 1144 KB Output is correct
13 Correct 6 ms 1016 KB Output is correct
14 Correct 8 ms 1016 KB Output is correct
15 Correct 8 ms 1016 KB Output is correct
16 Correct 8 ms 1016 KB Output is correct
17 Correct 40 ms 2552 KB Output is correct
18 Correct 34 ms 2572 KB Output is correct
19 Correct 35 ms 2552 KB Output is correct
20 Correct 10 ms 1016 KB Output is correct
21 Correct 9 ms 1016 KB Output is correct
22 Correct 6 ms 1016 KB Output is correct
23 Correct 6 ms 1016 KB Output is correct
24 Correct 5 ms 760 KB Output is correct
25 Correct 29 ms 2552 KB Output is correct
26 Correct 30 ms 2552 KB Output is correct
27 Correct 39 ms 2552 KB Output is correct
28 Correct 35 ms 2552 KB Output is correct
29 Correct 35 ms 2552 KB Output is correct
30 Correct 38 ms 2552 KB Output is correct
31 Correct 8 ms 1016 KB Output is correct
32 Correct 6 ms 1016 KB Output is correct
33 Correct 33 ms 2168 KB Output is correct
34 Correct 6 ms 1016 KB Output is correct
35 Correct 29 ms 2680 KB Output is correct
36 Correct 5 ms 760 KB Output is correct
37 Correct 5 ms 888 KB Output is correct
38 Correct 28 ms 2552 KB Output is correct
39 Correct 27 ms 2552 KB Output is correct
40 Correct 29 ms 2552 KB Output is correct
41 Correct 28 ms 2552 KB Output is correct
42 Correct 28 ms 2552 KB Output is correct
43 Correct 5 ms 760 KB Output is correct
44 Correct 5 ms 760 KB Output is correct
45 Correct 5 ms 760 KB Output is correct
46 Correct 5 ms 760 KB Output is correct
47 Correct 5 ms 760 KB Output is correct
48 Correct 5 ms 760 KB Output is correct
49 Correct 27 ms 2552 KB Output is correct
50 Correct 29 ms 2552 KB Output is correct
51 Correct 28 ms 2552 KB Output is correct
52 Correct 28 ms 2552 KB Output is correct
53 Correct 36 ms 2552 KB Output is correct
54 Correct 44 ms 2552 KB Output is correct
55 Correct 40 ms 2556 KB Output is correct
56 Correct 8 ms 1016 KB Output is correct
57 Correct 7 ms 1016 KB Output is correct
58 Correct 377 ms 2296 KB Output is correct
59 Correct 372 ms 2272 KB Output is correct
60 Correct 374 ms 2296 KB Output is correct
61 Correct 300 ms 2220 KB Output is correct
62 Correct 371 ms 2300 KB Output is correct
63 Correct 364 ms 2168 KB Output is correct
64 Correct 324 ms 2168 KB Output is correct
65 Correct 324 ms 2168 KB Output is correct
66 Correct 325 ms 2172 KB Output is correct
67 Correct 21 ms 2040 KB Output is correct
68 Correct 32 ms 2552 KB Output is correct
69 Correct 32 ms 2552 KB Output is correct
70 Correct 41 ms 2552 KB Output is correct
71 Correct 39 ms 2552 KB Output is correct
72 Correct 38 ms 2556 KB Output is correct
73 Correct 38 ms 2436 KB Output is correct
74 Correct 43 ms 2680 KB Output is correct