Submission #296558

# Submission time Handle Problem Language Result Execution time Memory
296558 2020-09-10T16:13:29 Z matt_H Two Transportations (JOI19_transportations) C++14
6 / 100
1138 ms 20256 KB
#include "Azer.h"
#include <cstdio>
#include <vector>
#include <queue>

typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
typedef std::vector<vii> vvii;

namespace {
    int N, done;
    vi dist, alive;
    vvii neighbors;
    std::priority_queue<pii> dijkQ;
    const int INF = 1000000000;
    const int INF_W = 511;

    int reg;
    int other_dist, cur_dist;
    int expected, cnt;
    enum Event { receivedDist, receivedPoint } event;
    void receiveInt(int n, Event e) {
        expected = n;
        cnt = 0;
        reg = 0;
        event = e;
    }

    void sendInt(int n, int to_send) {
        for (int i = 0; i < n; i++) {
            SendA(to_send & 1);
            to_send >>= 1;
        }
    }

    pii peek(std::priority_queue<pii> &q) {
        if (q.empty())
            return {INF_W, -1};
        pii rst;
        rst = q.top();
        while (!alive[rst.second]) {
            q.pop();
            if (q.empty())
                return {INF_W, -1};
            rst = q.top();
        }
        return {(-rst.first) - cur_dist, rst.second};
    }

    pii holding;

    void dijk_step() {
        // holding is the to-be-expanded node
        cur_dist += holding.first;
        int cur_node = holding.second;
        dist[cur_node] = cur_dist;
        done++;
        alive[cur_node] = false;
//        fprintf(stderr, "Alice does dijk on %d with dist %d\n", cur_node, cur_dist);

        if (done == N)
            return;

        for (auto it : neighbors[cur_node])
            if (alive[it.second] && cur_dist + it.first < dist[it.second]) {
                dist[it.second] = cur_dist + it.first;
                dijkQ.emplace(-(cur_dist + it.first), it.second);
            }

        holding = peek(dijkQ);

        sendInt(9, holding.first);
        receiveInt(9, receivedDist);
    }

    void compare_with_other() {
        other_dist = reg;
//        fprintf(stderr, "Alice compare her %d on node %d from Bob's dist %d\n",
//                holding.first, holding.second, other_dist);
        if (other_dist < holding.first)
            receiveInt(11, receivedPoint);
        else {
            sendInt(11, holding.second);
            dijk_step();
        }
    }

    void envoke_handler() {
       switch(event) {
           case receivedDist:
               compare_with_other();
               break;
           case receivedPoint:
//               fprintf(stderr, "Alice received from Bob the point %d\n", reg);
               holding.first = other_dist;
               holding.second = reg;
               dijk_step();
       }
    }

}  // namespace

void InitA(int N, int A, std::vector<int> U, std::vector<int> V,
           std::vector<int> C) {
    ::N = N;
    done = 0;
    neighbors.assign(N, vii());
    dist.assign(N, INF);
    alive.assign(N, true);
    for (int i = 0; i < A; i++) {
        neighbors[U[i]].emplace_back(C[i], V[i]);
        neighbors[V[i]].emplace_back(C[i], U[i]);
    }

    holding.first = holding.second = 0;
    cur_dist = 0;
    dijk_step();
}

void ReceiveA(bool x) {
    expected--;
    if (expected < 0)
        fprintf(stderr, "unexpected msg from B.");
    else {
        // this is the cnt-th bit received
        reg |= (x << cnt);
    }
    cnt++;
    if (expected == 0) // transferred fin
        envoke_handler();
}

std::vector<int> Answer() {
    return dist;
}
#include "Baijan.h"
#include <cstdio>
#include <vector>
#include <queue>

typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
typedef std::vector<vii> vvii;

namespace {
    int N, done;
    vi dist, alive;
    vvii neighbors;
    std::priority_queue<pii> dijkQ;
    const int INF = 1000000000;
    const int INF_W = 511;

    int reg;
    int other_dist, cur_dist;
    int expected, cnt;
    enum Event { receivedDist, receivedPoint } event;
    void receiveInt(int n, Event e) {
        expected = n;
        cnt = 0;
        reg = 0;
        event = e;
    }

    void sendInt(int n, int to_send) {
        for (int i = 0; i < n; i++) {
            SendB(to_send & 1);
            to_send >>= 1;
        }
    }

    pii peek(std::priority_queue<pii> &q) {
        if (q.empty())
            return {INF_W, -1};
        pii rst;
        rst = q.top();
        while (!alive[rst.second]) {
            q.pop();
            if (q.empty())
                return {INF_W, -1};
            rst = q.top();
        }
        return {(-rst.first) - cur_dist, rst.second};
    }

    pii holding;

    void dijk_step() {
        // holding is the to-be-expanded node
        cur_dist += holding.first;
        int cur_node = holding.second;
        dist[cur_node] = cur_dist;
        done++;
        alive[cur_node] = false;
//        fprintf(stderr, "%d-th iteration Bob does dijk on %d with dist %d\n", done, cur_node, cur_dist);

        if (done == N)
            return;

        for (auto it : neighbors[cur_node])
            if (alive[it.second] && cur_dist + it.first < dist[it.second]) {
//                fprintf(stderr, "Bob updating %d to %d\n", it.second, cur_dist + it.first);
                dist[it.second] = cur_dist + it.first;
                dijkQ.emplace(-(cur_dist + it.first), it.second);
            }

        holding = peek(dijkQ);
        sendInt(9, holding.first);
        receiveInt(9, receivedDist);

    }

    void compare_with_other() {
        other_dist = reg;
//        fprintf(stderr, "Bob compare his %d on node %d from Alice's dist %d\n",
//                holding.first, holding.second, other_dist);
        if (other_dist < holding.first)
            receiveInt(11, receivedPoint);
        else {
            sendInt(11, holding.second);
            dijk_step();
        }
    }

    void envoke_handler() {
        switch(event) {
            case receivedDist:
                compare_with_other();
                break;
            case receivedPoint:
                holding.first = other_dist;
                holding.second = reg;
                dijk_step();
        }
    }

}  // namespace

void InitB(int N, int B, std::vector<int> S, std::vector<int> T,
           std::vector<int> D) {
    ::N = N;
    done = 0;
    neighbors.assign(N, vii());
    dist.assign(N, INF);
    alive.assign(N, true);
    for (int i = 0; i < B; i++) {
        neighbors[S[i]].emplace_back(D[i], T[i]);
        neighbors[T[i]].emplace_back(D[i], S[i]);
    }

    holding.first = holding.second = 0;
    cur_dist = 0;
    dijk_step();
}

void ReceiveB(bool x) {
    expected--;
    if (expected < 0)
        fprintf(stderr, "unexpected msg from A.");
    else {
        // this is the cnt-th bit received
        reg |= (x << cnt);
    }
    cnt++;
    if (expected == 0) // transferred fin
        envoke_handler();
}
# Verdict Execution time Memory Grader output
1 Correct 1002 ms 2016 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 1036 ms 1840 KB Output is correct
4 Correct 1138 ms 20256 KB Output is correct
5 Correct 58 ms 1536 KB Output is correct
6 Correct 972 ms 4832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1088 KB Output is correct
2 Correct 902 ms 1536 KB Output is correct
3 Failed 264 ms 1136 KB Unexpected end of file - int32 expected (Baijan)
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 146 ms 1136 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Failed 21 ms 788 KB Unexpected end of file - int32 expected (Baijan)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Failed 21 ms 788 KB Unexpected end of file - int32 expected (Baijan)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Failed 21 ms 788 KB Unexpected end of file - int32 expected (Baijan)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1002 ms 2016 KB Output is correct
2 Correct 2 ms 1024 KB Output is correct
3 Correct 1036 ms 1840 KB Output is correct
4 Correct 1138 ms 20256 KB Output is correct
5 Correct 58 ms 1536 KB Output is correct
6 Correct 972 ms 4832 KB Output is correct
7 Correct 4 ms 1088 KB Output is correct
8 Correct 902 ms 1536 KB Output is correct
9 Failed 264 ms 1136 KB Unexpected end of file - int32 expected (Baijan)
10 Halted 0 ms 0 KB -