QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#808004#7701. A (Fast) Walk in the WoodsKITKAT (Ryuma Noma, Shinryu Tachibana, Souma Takao)AC ✓80ms4264kbC++204.8kb2024-12-10 16:03:532024-12-10 16:03:53

Judging History

This is the latest submission verdict.

  • [2024-12-10 16:03:53]
  • Judged
  • Verdict: AC
  • Time: 80ms
  • Memory: 4264kb
  • [2024-12-10 16:03:53]
  • Submitted

answer

#include <bits/stdc++.h>
#define dbg(x) cerr << #x << ':' << (x) << endl; 
using namespace std;
using ll = long long;
using ld = long double;
#define ALL(x) x.begin(),x.end()

int n,m;
vector<int> x,y;
vector<vector<array<int,3>>> g;
int start,sdir;

vector<vector<bool>> vis;
vector<int> cnt;
vector<pair<int,int>> his;
int rev[] = {2,3,0,1};

array<int,3> select(int pos, int dir, int dec) {
    if (dec >= 0 && g[pos][dec][1] > 0) {
        int to = g[pos][dec][0];
        int cap = g[pos][dec][1];
        return {to,dec,cap};
    }
    vector<int> cand;
    for (int i = 0; i < 4; ++i) if (i != rev[dir]) {
        if (g[pos][i][1] > 0) {
            cand.push_back(i);
        }
    }
    // cerr << "cand: ";
    // for (int i : cand) cerr << i << "(" << g[pos][i][1] << ")" << ' ';
    // cerr << '\n';

    if (cand.size() == 0) {
        return {-1,-1,-1};
    }
    else if (cand.size() == 1) {
        int nd = cand[0];
        int to  = g[pos][nd][0];
        int cap = g[pos][nd][1];
        return {to,nd,cap};
    }
    else if (cand.size() == 2) {
        int nd;
        for (int i = 1; i <= 3; ++i) {
            nd = (rev[dir] + i) % 4;
            if (g[pos][nd][1] > 0) {
                break;
            }
        }
        int to = g[pos][nd][0];
        int cap = g[pos][nd][1];
        return {to,nd,cap};
    }
    else if (cand.size() == 3) {
        int nd;
        bool first = true;
        for (int i = 1; i <= 3; ++i) {
            nd = (rev[dir] + i) % 4;
            if (g[pos][nd][1] > 0) {
                if (first) {
                    first = false;
                }
                else {
                    break;
                }
            }
        }
        int to = g[pos][nd][0];
        int cap = g[pos][nd][1];
        return {to,nd,cap};
    }
    else {
        assert(false);
    }
}

array<int,3> sim(int pos, int dir) {
    int dec = dir;
    while (true) {
        // cerr << "[" << pos << ' ' << dir << "]\n";
        // cerr << "[" << x[pos] << ' ' << y[pos] << "]\n";

        auto [nx,ndir,cap] = select(pos, dir, dec);
        if (nx == -1) {
            return {1,pos,dir};
        }

        if (vis[pos][ndir]) {
            break;
        }
        else {
            g[pos][ndir][1] -= 1;
            int to = g[pos][ndir][0];
            g[to][rev[ndir]][1] -= 1;

            int eid = g[pos][ndir][2];
            cnt[eid] += 1;

            his.emplace_back(pos, ndir);
            vis[pos][ndir] = true;

            pos = nx;
            dir = ndir;
        }
        dec = -1;
    }
    int mi = 1e9;
    for (auto [p,d] : his) {
        int eid = g[p][d][2];
        assert(cnt[eid] != 0);
        mi = min(mi, g[p][d][1] / cnt[eid]);
    }
    // dbg(mi)

    for (auto [p,d] : his) {
        int eid = g[p][d][2];
        g[p][d][1] -= mi * cnt[eid];
        int to = g[p][d][0];
        g[to][rev[d]][1] -= mi * cnt[eid];

        cnt[eid] = 0;
        vis[p][d] = false;
    }
    his.clear();
    for (int i = 0; i < n; ++i) for (int j = 0; j < 4; ++j) assert(vis[i][j] == 0);
    return {0,pos,dir};
}

int main()
{
    cin >> n >> m;
    x.resize(n);
    y.resize(n);
    g.resize(n, vector<array<int,3>>(4));
    vis.resize(n, vector<bool>(4));
    cnt.resize(m);
    for (int i = 0; i < n; ++i) cin >> x[i] >> y[i];
    for (int i = 0; i < m; ++i) {
        int a,b,c;
        cin >> a >> b >> c;
        --a; --b;
        if (x[a] > x[b]) {
            g[a][3][0] = b;
            g[b][1][0] = a;
            g[a][3][1] = g[b][1][1] = c;
            g[a][3][2] = g[b][1][2] = i;
        }
        else if (x[a] < x[b]) {
            g[a][1][0] = b;
            g[b][3][0] = a;
            g[a][1][1] = g[b][3][1] = c;
            g[a][1][2] = g[b][3][2] = i;
        }
        else if (y[a] > y[b]) {
            g[a][2][0] = b;
            g[b][0][0] = a;
            g[a][2][1] = g[b][0][1] = c;
            g[a][2][2] = g[b][0][2] = i;
        }
        else if (y[a] < y[b]) {
            g[a][0][0] = b;
            g[b][2][0] = a;
            g[a][0][1] = g[b][2][1] = c;
            g[a][0][2] = g[b][2][2] = i;
        }
        else {
            assert(false);
        }
    }
    char c;
    cin >> start >> c;
    --start;
    if (c == 'N') sdir = 0;
    else if (c == 'E') sdir = 1;
    else if (c == 'S') sdir = 2;
    else sdir = 3;

    int pos = start, dir = sdir;
    while (true) {
        // cerr << pos << ' ' << dir << endl;
        // cerr << x[pos] << ' ' << y[pos] << endl;
        auto [end, nx, ndir] = sim(pos, dir);
        // cerr << end << ' ' << nx << ' ' << ndir << '\n';
        if (end) {
            cout << x[nx] << ' ' << y[nx] << '\n';
            return 0;
        }
        pos = nx;
        dir = ndir;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

7 8
0 0 5 0 12 0 0 5 5 5 0 10 12 10
1 2 2
2 3 4
4 5 5
6 7 8
1 4 4
2 5 7
3 7 4
4 6 6
4 N

output:

0 0

result:

ok single line: '0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

2 1
5 5 100 5
1 2 10000
2 W

output:

5 5

result:

ok single line: '5 5'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

4 4
0 0 0 10 10 10 10 0
1 2 10
2 3 10
3 4 10
1 4 10
4 N

output:

10 0

result:

ok single line: '10 0'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

9 12
0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2
1 2 1
2 3 1
4 1 1
4 5 1
5 2 1
5 6 1
6 3 1
6 9 1
9 8 1
8 5 1
8 7 1
7 4 1
8 N

output:

1 0

result:

ok single line: '1 0'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

9 12
0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2
1 2 1
2 3 1
4 1 1
4 5 1
5 2 1
5 6 1
6 3 1
6 9 1
9 8 1
8 5 1
8 7 1
7 4 1
8 W

output:

0 1

result:

ok single line: '0 1'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

4 4
0 0 0 5 5 5 5 0
1 2 1
2 3 1
3 4 1
4 1 1
1 E

output:

0 0

result:

ok single line: '0 0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

4 4
0 0 0 5 5 5 5 0
1 2 1
2 3 1
3 4 1
4 1 1
1 N

output:

0 0

result:

ok single line: '0 0'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

4 4
0 0 0 5 5 5 5 0
1 2 10000
2 3 10000
3 4 10000
4 1 10000
1 E

output:

0 0

result:

ok single line: '0 0'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

4 4
0 0 0 5 5 5 5 0
1 2 101
2 3 101
3 4 100
4 1 100
1 E

output:

0 0

result:

ok single line: '0 0'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

4 4
0 0 0 5 5 5 5 0
1 2 101
2 3 100
3 4 101
4 1 100
1 E

output:

0 0

result:

ok single line: '0 0'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

8 9
0 20 10 20 0 10 10 10 20 10 30 10 20 0 30 0
1 2 10
3 4 10
4 5 11
5 6 10
7 8 10
1 3 10
2 4 10
5 7 10
6 8 10
4 E

output:

20 10

result:

ok single line: '20 10'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

8 9
0 20 10 20 0 10 10 10 20 10 30 10 20 0 30 0
1 2 10
3 4 10
4 5 10
5 6 10
7 8 10
1 3 10
2 4 10
5 7 10
6 8 10
4 E

output:

10 10

result:

ok single line: '10 10'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

8 7
0 53 10 17 25 8 25 17 37 0 0 8 10 0 37 53
1 8 100
4 2 200
6 3 100
1 6 100
7 2 100
4 3 100
5 8 100
7 N

output:

37 0

result:

ok single line: '37 0'

Test #14:

score: 0
Accepted
time: 0ms
memory: 4124kb

input:

2400 2999
0 0 1 0 1 1 0 1 2 0 3 0 3 1 2 1 4 0 5 0 5 1 4 1 6 0 7 0 7 1 6 1 8 0 9 0 9 1 8 1 10 0 11 0 11 1 10 1 12 0 13 0 13 1 12 1 14 0 15 0 15 1 14 1 16 0 17 0 17 1 16 1 18 0 19 0 19 1 18 1 20 0 21 0 21 1 20 1 22 0 23 0 23 1 22 1 24 0 25 0 25 1 24 1 26 0 27 0 27 1 26 1 28 0 29 0 29 1 28 1 30 0 31 0 ...

output:

0 1

result:

ok single line: '0 1'

Test #15:

score: 0
Accepted
time: 48ms
memory: 4248kb

input:

2500 3748
0 0 0 10 10 0 10 10 20 0 20 10 30 0 30 10 40 0 40 10 50 0 50 10 60 0 60 10 70 0 70 10 80 0 80 10 90 0 90 10 100 0 100 10 110 0 110 10 120 0 120 10 130 0 130 10 140 0 140 10 150 0 150 10 160 0 160 10 170 0 170 10 180 0 180 10 190 0 190 10 200 0 200 10 210 0 210 10 220 0 220 10 230 0 230 10 ...

output:

12490 10

result:

ok single line: '12490 10'

Test #16:

score: 0
Accepted
time: 3ms
memory: 3988kb

input:

2500 2548
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 0...

output:

490 0

result:

ok single line: '490 0'

Test #17:

score: 0
Accepted
time: 3ms
memory: 4044kb

input:

2500 2524
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 0...

output:

240 250

result:

ok single line: '240 250'

Test #18:

score: 0
Accepted
time: 3ms
memory: 4264kb

input:

2500 4420
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 0...

output:

40 130

result:

ok single line: '40 130'

Test #19:

score: 0
Accepted
time: 0ms
memory: 4052kb

input:

2500 4373
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 5...

output:

70 70

result:

ok single line: '70 70'

Test #20:

score: 0
Accepted
time: 3ms
memory: 4256kb

input:

2500 3891
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 0 10 10 10 20 10 30 10 40 10 50 10 60 10 70 10 80 10 90 10 100 10 110 10 120 10 130 10 140 10 150 10 160 10 170 10 180 10 190 10 200 10 210 10 220 10 2...

output:

70 160

result:

ok single line: '70 160'

Test #21:

score: 0
Accepted
time: 3ms
memory: 3996kb

input:

2500 4352
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 5...

output:

300 190

result:

ok single line: '300 190'

Test #22:

score: 0
Accepted
time: 0ms
memory: 4032kb

input:

2500 4363
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 0 10 10 10 20 10 30 10 40 10 50 10 60 10 70 10 80 10 90 10 100 10 110 10 120 10 130 10 140 10 150 10 160 10 170 10 180 10 190 10 0 20 10 20 20 20 30 20 40 20 50 20 60 20 70 20 80 20...

output:

100 170

result:

ok single line: '100 170'

Test #23:

score: 0
Accepted
time: 3ms
memory: 4088kb

input:

2500 4043
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 5...

output:

130 90

result:

ok single line: '130 90'

Test #24:

score: 0
Accepted
time: 3ms
memory: 4052kb

input:

2500 2812
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 0 10 10 10 20 10 30 10 40 10 50 10 60 10 70 10 80 10 90 10 0 20 10 20 20 20 30 20 40 20 50 20 60 20 70 20 80 20 90 20 0 30 10 30 20 30 30 30 40 30 50 30 60 30 70 30 80 30 90 30 0 40 10 40 20 40 30 40 40 40 50 40 60 40 70 40 80 40 90 40 0 50 ...

output:

0 10

result:

ok single line: '0 10'

Test #25:

score: 0
Accepted
time: 3ms
memory: 4152kb

input:

2500 4009
0 0 10 0 20 0 30 0 40 0 50 0 60 0 70 0 80 0 90 0 100 0 110 0 120 0 130 0 140 0 150 0 160 0 170 0 180 0 190 0 200 0 210 0 220 0 230 0 240 0 250 0 260 0 270 0 280 0 290 0 300 0 310 0 320 0 330 0 340 0 350 0 360 0 370 0 380 0 390 0 400 0 410 0 420 0 430 0 440 0 450 0 460 0 470 0 480 0 490 0 5...

output:

300 30

result:

ok single line: '300 30'

Test #26:

score: 0
Accepted
time: 3ms
memory: 4056kb

input:

2500 4044
0 0 10 0 20 0 30 0 40 0 0 10 10 10 20 10 30 10 40 10 0 20 10 20 20 20 30 20 40 20 0 30 10 30 20 30 30 30 40 30 0 40 10 40 20 40 30 40 40 40 0 50 10 50 20 50 30 50 40 50 0 60 10 60 20 60 30 60 40 60 0 70 10 70 20 70 30 70 40 70 0 80 10 80 20 80 30 80 40 80 0 90 10 90 20 90 30 90 40 90 0 100...

output:

0 150

result:

ok single line: '0 150'

Test #27:

score: 0
Accepted
time: 80ms
memory: 4104kb

input:

2500 3748
0 998809 1000000 998809 0 998163 1000000 998163 0 996915 1000000 996915 0 996154 1000000 996154 0 994351 1000000 994351 0 993498 1000000 993498 0 992719 1000000 992719 0 992353 1000000 992353 0 991299 1000000 991299 0 991255 1000000 991255 0 990904 1000000 990904 0 989447 1000000 989447 0 ...

output:

1000000 0

result:

ok single line: '1000000 0'