Submission #851233

# Submission time Handle Problem Language Result Execution time Memory
851233 2023-09-19T04:38:54 Z vjudge1 Commuter Pass (JOI18_commuter_pass) C++17
100 / 100
282 ms 33172 KB
#include <bits/stdc++.h>
#define TASK "bus"
#define f first
#define s second

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ll> ill;
typedef pair<ll, int> lli;
typedef pair<ll, ll> pll;

const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;

class state
{
public:
        int v;
        ll w;
        bool cont, cur_cont;
        state() { v = w = cont = cur_cont = 0; }
        state(const int &I_v, const ll &I_w, const bool &I_cont, const bool &I_ccont)
        {
                v = I_v;
                w = I_w;
                cont = I_cont;
                cur_cont = I_ccont;
        }

        bool operator<(const state &SE) const { return w > SE.w; }
};

struct cmp
{
        bool operator()(ill FI, ill SE) { return FI.s > SE.s; }
};

int n, m, free_start, free_end, p_start, p_end;
vector<ii> e[MAX];
ll fdist[2][MAX];
priority_queue<ill, vector<ill>, cmp> qx;

void DIJKSTRA_BUILD(int start, int nt)
{
        for (int i = 1; i <= n; ++i)
                fdist[nt][i] = LLONG_MAX;
        fdist[nt][start] = 0;
        qx.push({start, 0});

        while (qx.size())
        {
                int cv = qx.top().f;
                ll cw = qx.top().s;
                qx.pop();
                if (fdist[nt][cv] < cw)
                        continue;

                for (auto itr : e[cv])
                {
                        int nv = itr.f, nxw = itr.s;
                        if (fdist[nt][nv] > cw + nxw)
                        {
                                fdist[nt][nv] = cw + nxw;
                                qx.push({nv, cw + nxw});
                        }
                }
        }
}

ll rdist[MAX][2][2];
priority_queue<state> qrx;
ll result = LLONG_MAX;

bool IN_PATH(int v1, int v2, int w)
{
        return fdist[0][v1] + fdist[1][v2] + w == fdist[0][free_end];
}

void DIJKSTRA_RUN(int z_start, int z_end)
{
        for (int i = 1; i <= n; ++i)
                for (int j = 0; j < 2; ++j)
                        rdist[i][j][0] = rdist[i][j][1] = LLONG_MAX;
        rdist[z_start][0][0] = 0;
        qrx.push(state(z_start, 0, 0, 0));
        while (qrx.size())
        {
                int cv = qrx.top().v;
                ll cw = qrx.top().w;
                bool cc1 = qrx.top().cont, cc2 = qrx.top().cur_cont;
                qrx.pop();
                if (cw > rdist[cv][cc1][cc2])
                        continue;
                if (cv == z_end)
                        break;

                for (auto itr : e[cv])
                {
                        int nv = itr.f, nxw = itr.s;
                        if (rdist[nv][cc1][0] > cw + nxw)
                        {
                                rdist[nv][cc1][0] = cw + nxw;
                                qrx.push(state(nv, cw + nxw, cc1, 0));
                        }
                        if (IN_PATH(cv, nv, nxw))
                        {
                                if (cc1 && !cc2)
                                        continue;
                                if (rdist[nv][1][1] > cw)
                                {
                                        rdist[nv][1][1] = cw;
                                        qrx.push(state(nv, cw, 1, 1));
                                }
                        }
                }
        }
        result = min({result, rdist[z_end][1][1], rdist[z_end][1][0], rdist[z_end][0][0]});
}

int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);

        if (fopen(TASK ".inp", "r"))
        {
                freopen(TASK ".inp", "r", stdin);
                freopen(TASK ".out", "w", stdout);
        }

        cin >> n >> m >> free_start >> free_end >> p_start >> p_end;
        while (m--)
        {
                int u1, u2, uw;
                cin >> u1 >> u2 >> uw;
                e[u1].push_back({u2, uw});
                e[u2].push_back({u1, uw});
        }

        DIJKSTRA_BUILD(free_start, 0);
        DIJKSTRA_BUILD(free_end, 1);

        DIJKSTRA_RUN(p_start, p_end);
        DIJKSTRA_RUN(p_end, p_start);
        cout << result;
}

Compilation message

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:130:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |                 freopen(TASK ".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:131:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |                 freopen(TASK ".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 170 ms 23236 KB Output is correct
2 Correct 210 ms 22404 KB Output is correct
3 Correct 260 ms 27652 KB Output is correct
4 Correct 184 ms 22908 KB Output is correct
5 Correct 278 ms 26908 KB Output is correct
6 Correct 194 ms 26620 KB Output is correct
7 Correct 168 ms 27020 KB Output is correct
8 Correct 208 ms 25516 KB Output is correct
9 Correct 139 ms 23752 KB Output is correct
10 Correct 141 ms 27032 KB Output is correct
11 Correct 70 ms 14288 KB Output is correct
12 Correct 165 ms 22572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 17092 KB Output is correct
2 Correct 159 ms 26992 KB Output is correct
3 Correct 237 ms 27000 KB Output is correct
4 Correct 222 ms 26800 KB Output is correct
5 Correct 147 ms 21860 KB Output is correct
6 Correct 275 ms 25776 KB Output is correct
7 Correct 242 ms 25772 KB Output is correct
8 Correct 214 ms 26296 KB Output is correct
9 Correct 145 ms 23076 KB Output is correct
10 Correct 123 ms 23144 KB Output is correct
11 Correct 75 ms 16072 KB Output is correct
12 Correct 207 ms 25724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5980 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 1 ms 5212 KB Output is correct
4 Correct 11 ms 7280 KB Output is correct
5 Correct 6 ms 6236 KB Output is correct
6 Correct 1 ms 5212 KB Output is correct
7 Correct 2 ms 5356 KB Output is correct
8 Correct 2 ms 5468 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 6 ms 6236 KB Output is correct
11 Correct 1 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5260 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 23236 KB Output is correct
2 Correct 210 ms 22404 KB Output is correct
3 Correct 260 ms 27652 KB Output is correct
4 Correct 184 ms 22908 KB Output is correct
5 Correct 278 ms 26908 KB Output is correct
6 Correct 194 ms 26620 KB Output is correct
7 Correct 168 ms 27020 KB Output is correct
8 Correct 208 ms 25516 KB Output is correct
9 Correct 139 ms 23752 KB Output is correct
10 Correct 141 ms 27032 KB Output is correct
11 Correct 70 ms 14288 KB Output is correct
12 Correct 165 ms 22572 KB Output is correct
13 Correct 119 ms 17092 KB Output is correct
14 Correct 159 ms 26992 KB Output is correct
15 Correct 237 ms 27000 KB Output is correct
16 Correct 222 ms 26800 KB Output is correct
17 Correct 147 ms 21860 KB Output is correct
18 Correct 275 ms 25776 KB Output is correct
19 Correct 242 ms 25772 KB Output is correct
20 Correct 214 ms 26296 KB Output is correct
21 Correct 145 ms 23076 KB Output is correct
22 Correct 123 ms 23144 KB Output is correct
23 Correct 75 ms 16072 KB Output is correct
24 Correct 207 ms 25724 KB Output is correct
25 Correct 6 ms 5980 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 11 ms 7280 KB Output is correct
29 Correct 6 ms 6236 KB Output is correct
30 Correct 1 ms 5212 KB Output is correct
31 Correct 2 ms 5356 KB Output is correct
32 Correct 2 ms 5468 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 6 ms 6236 KB Output is correct
35 Correct 1 ms 5208 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5260 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 202 ms 28196 KB Output is correct
41 Correct 144 ms 24012 KB Output is correct
42 Correct 155 ms 22632 KB Output is correct
43 Correct 134 ms 15816 KB Output is correct
44 Correct 81 ms 15752 KB Output is correct
45 Correct 282 ms 31872 KB Output is correct
46 Correct 210 ms 24608 KB Output is correct
47 Correct 221 ms 27036 KB Output is correct
48 Correct 97 ms 15948 KB Output is correct
49 Correct 217 ms 33172 KB Output is correct
50 Correct 202 ms 26600 KB Output is correct
51 Correct 152 ms 26048 KB Output is correct