QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#354222#4171. 魔法猪学院james1BadCreeper100 ✓63ms91440kbC++142.8kb2024-03-14 23:52:222024-03-14 23:52:23

Judging History

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

  • [2024-03-14 23:52:23]
  • 评测
  • 测评结果:100
  • 用时:63ms
  • 内存:91440kb
  • [2024-03-14 23:52:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5; 
const int M = 2e5 + 5; 
const int INF = 1e9; 

int n, m, vis[N]; 
double ET, dn[N]; 
struct Edge { int v; double w; int id; Edge(int v = 0, double w = 0, int id = 0) : v(v), w(w), id(id) {} }; 
vector<Edge> G[N], E[N]; 

void init(void) {
    #define pii pair<double, int>
    priority_queue<pii, vector<pii>, greater<pii>> q; 
    for (int i = 1; i <= n; ++i) dn[i] = INF; 
    q.emplace(dn[n] = 0, n); 
    while (!q.empty()) {
        int u = q.top().second; q.pop(); 
        if (vis[u]) continue; vis[u] = 1; 
        for (auto [v, w, id] : E[u]) if (dn[v] > dn[u] + w) q.emplace(dn[v] = dn[u] + w, v); 
    }
    if (dn[1] >= INF) cout << "0\n", exit(0); 
    if (dn[1] >= ET) cout << "1\n", exit(0); 
}

int fa[N]; 
bool tree[M]; 
void dfs(int x) {
    vis[x] = 1; 
    for (auto [y, w, id] : E[x]) if (!vis[y] && dn[y] == dn[x] + w) fa[y] = x, tree[id] = 1, dfs(y); 
}

struct Node {
    int x; double v; 
    Node(int x = 0, double v = 0) : x(x), v(v) {}
    bool operator> (const Node &a) const {
        return v > a.v; 
    }
}; 
int ls[M * 20], rs[M * 20], d[M * 20], tot, rt[N]; 
Node val[M * 20]; 
int newNode(Node w) { return val[++tot] = w, tot; }
int merge(int x, int y) {
    if (!x || !y) return x + y; 
    if (val[x] > val[y]) swap(x, y); 
    int p = ++tot; val[p] = val[x]; ls[p] = ls[x]; 
    rs[p] = merge(rs[x], y); 
    if (d[ls[p]] < d[rs[p]]) swap(ls[p], rs[p]); 
    d[p] = d[rs[p]] + 1; 
    return p; 
}

void dfs2(int x) {
    vis[x] = 1; 
    if (fa[x]) rt[x] = merge(rt[x], rt[fa[x]]); 
    for (auto [y, w, id] : E[x]) if (fa[y] == x && !vis[y]) dfs2(y); 
}

int main(void) {
    ios::sync_with_stdio(0); 

    cin >> n >> m >> ET; 
    for (int i = 1; i <= m; ++i) {
        int u, v; double w; cin >> u >> v >> w; 
        if (u == n) continue; 
        G[u].emplace_back(v, w, i); 
        E[v].emplace_back(u, w, i); 
    }
    init(); 
    memset(vis, 0, sizeof vis); dfs(n); 

    for (int u = 1; u <= n; ++u) if (dn[u] < INF)
        for (auto [v, w, id] : G[u]) if (!tree[id] && dn[v] < INF)
            rt[u] = merge(rt[u], newNode(Node(v, w - (dn[u] - dn[v])))); 
        
    memset(vis, 0, sizeof vis); dfs2(n); 

    int cnt = 0; 
    priority_queue<pii, vector<pii>, greater<pii>> q; 
    if (rt[1]) q.emplace(dn[1] + val[rt[1]].v, rt[1]); 
    double now = dn[1]; int k = 1; 
    while (!q.empty()) {
        auto [res, x] = q.top(); q.pop(); now += res; 
        if (now > ET) return cout << k << "\n", 0; ++k; 
        if (ls[x]) q.emplace(res - val[x].v + val[ls[x]].v, ls[x]); 
        if (rs[x]) q.emplace(res - val[x].v + val[rs[x]].v, rs[x]); 
        int u = val[x].x; 
        if (rt[u]) q.emplace(res + val[rt[u]].v, rt[u]); 
    }
    cout << k << "\n"; 
    return 0;
}

詳細信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 66536kb

input:

6 14 66666
3 4 31210.2
4 5 11207.2
5 2 19087.1
1 3 25736.9
2 6 10925.6
5 2 20617.1
2 4 8577.14
1 5 7859.54
4 6 29233.4
3 2 5416.09
2 5 10870.9
5 4 8595.81
6 3 30738.3
4 1 4210.31

output:

1

result:

ok single line: '1'

Test #2:

score: 10
Accepted
time: 0ms
memory: 66484kb

input:

30 96 100
9 26 4
26 22 5
22 10 6
10 6 7
6 21 9
21 11 4
11 17 10
17 2 10
2 18 9
18 28 5
28 4 9
4 29 6
29 25 8
25 20 6
20 14 10
14 5 5
5 23 7
23 15 6
1 9 7
15 30 10
16 24 10
24 9 7
9 21 3
21 10 3
10 7 3
7 18 9
18 20 10
20 12 3
12 27 8
27 3 4
3 23 4
23 28 10
28 8 4
8 15 3
15 17 3
17 5 4
5 6 10
6 2 6
2 ...

output:

4

result:

ok single line: '4'

Test #3:

score: 10
Accepted
time: 4ms
memory: 66576kb

input:

99 641 99
42 5 7
5 14 6
14 11 8
11 77 4
77 36 5
36 86 2
86 93 7
93 97 2
97 50 3
50 76 5
76 3 4
3 61 3
61 96 4
96 45 6
45 72 7
72 67 9
67 39 7
39 35 3
35 54 8
54 75 6
75 38 7
38 66 3
66 88 8
88 92 6
92 80 2
80 89 2
89 91 5
91 53 8
53 34 8
34 74 9
74 68 3
68 52 9
52 49 1
49 37 6
37 9 1
9 4 6
4 23 7
23...

output:

8

result:

ok single line: '8'

Test #4:

score: 10
Accepted
time: 63ms
memory: 91440kb

input:

1000 181296 999999
8 650 743998
650 320 834464
320 664 488124
664 411 814784
411 887 825242
887 322 881390
322 854 695099
854 719 343142
719 425 765369
425 585 594854
585 874 874574
874 843 544404
843 87 599144
87 128 930905
128 914 206730
914 461 159790
461 963 719162
963 751 717754
751 287 335806
...

output:

2

result:

ok single line: '2'

Test #5:

score: 10
Accepted
time: 37ms
memory: 79188kb

input:

1000 90509 777
832 913 1.94107
913 650 1.69906
650 202 1.33482
202 60 1.4781
60 238 1.52459
238 177 1.50846
177 309 1.54601
309 316 1.77919
316 335 1.75991
335 748 1.1945
748 598 1.66309
598 513 1.87494
513 427 1.83812
427 728 1.06084
728 540 1.85968
540 553 1.18499
553 288 1.35035
288 291 1.32966
2...

output:

225

result:

ok single line: '225'

Test #6:

score: 10
Accepted
time: 12ms
memory: 70212kb

input:

2222 16156 98765
69 963 6.98159
963 1999 5.60671
1999 108 8.545
108 1258 4.08367
1258 1709 4.66681
1709 90 7.35292
90 1300 5.95097
1300 1623 8.72925
1623 139 4.89662
139 423 8.28057
423 1235 6.44084
1235 13 6.82704
13 670 5.74766
670 365 8.28991
365 324 8.73557
324 2195 6.35717
2195 1747 6.16023
174...

output:

2014

result:

ok single line: '2014'

Test #7:

score: 10
Accepted
time: 19ms
memory: 71108kb

input:

3333 44115 99999
653 2442 46.4009
2442 297 73.3315
297 1959 94.7193
1959 2923 20.1875
2923 1831 80.6127
1831 1677 64.5939
1677 1809 38.0042
1809 3193 49.9268
3193 708 22.7042
708 2580 73.3195
2580 3154 18.7393
3154 1163 61.1456
1163 762 13.5259
762 420 50.3727
420 633 79.6002
633 247 99.5519
247 266...

output:

441

result:

ok single line: '441'

Test #8:

score: 10
Accepted
time: 13ms
memory: 70928kb

input:

5000 25014 999999
338 3873 1.20593
3873 3400 1.67902
3400 3658 1.17251
3658 3594 1.89888
3594 4753 1.59833
4753 1502 1.6123
1502 2304 1.496
2304 483 1.54973
483 4001 1.16819
4001 29 1.03435
29 3351 1.44136
3351 1970 1.60173
1970 1611 1.3438
1611 614 1.48917
614 1372 1.86184
1372 4755 1.54248
4755 22...

output:

57948

result:

ok single line: '57948'

Test #9:

score: 10
Accepted
time: 20ms
memory: 71112kb

input:

5000 28666 999983
11 1640 1.45937
1640 2345 2.32915
2345 2021 1.94338
2021 3616 1.14568
3616 763 2.09215
763 3527 2.10157
3527 1128 1.61896
1128 3439 1.22679
3439 269 2.30751
269 1988 2.07579
1988 1503 2.60381
1503 1122 1.99496
1122 1808 2.76742
1808 2921 2.97836
2921 4963 1.1324
4963 2887 2.45812
2...

output:

46312

result:

ok single line: '46312'

Test #10:

score: 10
Accepted
time: 24ms
memory: 69884kb

input:

5000 24915 1000000
4631 707 1.85882
707 480 2.82335
480 1411 3.0055
1411 1845 3.18194
1845 3884 3.67791
3884 4882 1.43678
4882 679 3.41302
679 3407 3.36917
3407 259 1.66206
259 1652 2.74687
1652 958 3.22253
958 1052 3.26796
1052 2706 2.51034
2706 4773 2.83871
4773 253 3.74137
253 4109 1.04059
4109 2...

output:

36693

result:

ok single line: '36693'