QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#50699#1274. Walking Planckiseki#AC ✓463ms8616kbC++3.3kb2022-09-28 17:36:402022-09-28 17:36:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-28 17:36:43]
  • 评测
  • 测评结果:AC
  • 用时:463ms
  • 内存:8616kb
  • [2022-09-28 17:36:40]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

const int C = 400;
const int INF = 1e9;

vector<int> Zalgo(const vector<int> &s) {
    vector<int> z(s.size(), s.size());
    for (int i = 1, l = 0, r = 0; i < z[0]; ++i) {
        int j = clamp(r - i, 0, z[i - l]);
        for (; i + j < z[0] and s[i + j] == s[j]; ++j);
        if (i + (z[i] = j) > r) r = i + z[l = i];
    }
    return z;
}

const int D = 50;

pair<int,int> find_cycle(vector<int> v) {
    assert (v.size() == C);
    for (int i = v.size() - 2; i >= 0; i--) {
        v[i] = min(v[i], v[i + 1]);
    }
    v.resize(C - D);
    // for (int k = 0; k < C; k++) {
    //     cerr << v[k] << ' ';
    // }
    // cerr << endl;
    for (int i = v.size() - 1; i >= 1; i--) {
        v[i] -= v[i - 1];
    }
    reverse(v.begin(), v.end());
    const auto Z = Zalgo(v);
    for (int k = 50; k >= 1; k--) {
        if (Z[k] >= k) {
            int s = 0;
            for (int i = 0; i < k; i++)
                s += v[i];
            return { k, s };
        }
    }
    __builtin_unreachable();
}

int g[50][50];
int dp[C][50];
vector<int> seq[50][50];
int cycle[50][50], csum[50][50];

int cdiv(int a, int b) {
    return (a + b - 1) / b;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int T; cin >> T;

    while (T--) {
        int n, m;
        cin >> n >> m;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                g[i][j] = INF;
            }
        }

        for (int i = 0; i < m; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            --u, --v;
            g[u][v] = min(g[u][v], w);
        }
        
        for (int s = 0; s < n; s++) {
            for (int k = 0; k < C; k++)
                for (int i = 0; i < n; i++)
                    dp[k][i] = INF;
            dp[0][s] = 0;

            for (int k = 0; k + 1 < C; k++)
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        dp[k + 1][j] = min(dp[k + 1][j], dp[k][i] + g[i][j]);

            for (int t = 0; t < n; t++) {
                seq[s][t].clear();
                for (int k = 0; k < C; k++) {
                    seq[s][t].push_back(dp[k][t]);
                }

                tie(cycle[s][t], csum[s][t]) = find_cycle(seq[s][t]);
            }
        }

        int q;
        cin >> q;
        while (q--) {
            int s, t, k;
            cin >> s >> t >> k;
            --s, --t;
            int ans = INF;
            // cerr << "-------\n";
            // for (int i = k; i < C; i++)
            //     cerr << seq[s][t][i] << ' ';
            // cerr << endl;
            // cerr << "-------\n";
            for (int i = k; i < C; i++) {
                ans = min(ans, seq[s][t][i]);
            }
            // cerr << "ans = " << ans << endl;
            for (int i = C - cycle[s][t] - D; i < C - D; i++) if (i < k) {
                int b = cdiv(k - i, cycle[s][t]);
                assert (b >= 1 and
                        i + b * cycle[s][t] >= k and
                        i + (b - 1) * cycle[s][t] < k);
                ans = min(ans, seq[s][t][i] + b * csum[s][t]);
            }
            if (ans == INF)
                cout << -1 << '\n';
            else
                cout << ans << '\n';
        }
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3848kb

input:

2
3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1

output:

111
1
11
-1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 278ms
memory: 8616kb

input:

10
5 10
4 2 55
2 1 26
3 4 83
5 2 16
4 5 54
3 4 38
2 1 68
2 5 1
4 5 85
4 2 60
20
2 1 13
1 4 17
3 2 20
2 4 16
4 2 17
4 2 2
3 1 2
1 5 10
2 1 8
4 5 15
4 2 16
3 1 18
5 2 7
4 2 9
4 3 16
1 4 18
3 2 5
1 5 9
5 1 19
1 2 16
20 50
4 16 25
3 16 990
9 18 863
19 12 236
4 10 360
13 4 585
14 17 164
8 18 198
2 17 804...

output:

128
-1
246
-1
191
70
119
-1
94
173
189
253
67
123
-1
-1
125
-1
195
-1
-1
23449
3476
18735
17412
23124
29499
26452
9757
21128
9524
-1
4486
8797
8041
33717
32669
5108
32534
13020
22800
4411
3529
37460
4191
15863
5342
22005
-1
14496
16535
13644
-1
33956
28717
24721
13816
26289
8840
28137
9991
14430
-1
...

result:

ok 406120 lines

Test #3:

score: 0
Accepted
time: 463ms
memory: 8612kb

input:

10
5 10
4 1 62
3 5 93
4 5 99
2 5 63
2 4 26
5 3 31
2 5 14
5 3 54
1 2 47
1 5 18
200
2 5 8
3 1 17
1 5 11
2 1 17
4 2 1
4 2 13
5 1 13
5 2 2
1 4 8
2 3 4
1 1 1
1 5 7
3 5 3
5 3 12
1 4 18
3 1 11
5 4 2
1 3 4
5 5 13
2 3 12
1 4 19
3 5 17
3 5 20
3 3 1
5 5 9
1 5 7
2 4 9
4 5 16
1 3 19
4 1 18
2 1 16
3 5 17
4 5 7
3 ...

output:

365
-1
466
763
109
649
-1
-1
343
137
135
288
217
775
883
-1
-1
173
868
531
883
1085
1333
124
620
288
431
744
848
872
763
1085
339
-1
343
-1
341
-1
161
784
-1
109
244
49
-1
424
871
-1
-1
872
587
393
270
763
61
701
620
-1
-1
270
558
-1
559
341
-1
540
135
-1
857
-1
-1
810
31
713
467
-1
-1
-1
277
784
69...

result:

ok 900200 lines