QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#4909#443. 翻修道路Qingyu100 ✓1317ms120152kbC++117.2kb2020-10-15 17:32:232021-12-19 05:35:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2021-12-19 05:35:34]
  • 评测
  • 测评结果:100
  • 用时:1317ms
  • 内存:120152kb
  • [2020-10-15 17:32:23]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template <typename T>
inline bool chkmin(T &a, const T &b) {
    return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
    return a < b ? a = b, 1 : 0;
}

typedef long long LL;

const int oo = 0x3f3f3f3f;

const int __buffsize = 100000;
char __buff[__buffsize];
char *__buffs, *__buffe;

#define getc()                                                                                \
    (__buffs == __buffe ? fread(__buff, 1, __buffsize, stdin), __buffe = __buff + __buffsize, \
     *((__buffs = __buff)++) : *(__buffs++))

template <typename T>
inline T &Read(T &x) {
    static char c;
    while (1) {
        c = getc();
        if (c == '-' || (c >= '0' && c <= '9'))
            break;
    }
    bool flag = c == '-';
    x = flag ? 0 : c - '0';
    while (1) {
        c = getc();
        if (c < '0' || c > '9')
            break;
        (x *= 10) += c - '0';
    }
    if (flag)
        x = -x;
    return x;
}

#undef getc

const int maxn = 500010, maxm = 1000100;

struct edge {
    int id, g, nxt;

    edge() {}
    edge(int _id, int _g, int _nxt) : id(_id), g(_g), nxt(_nxt) {}
};

int en;
int st[maxn + 5];
edge e[(maxm << 1) + 5];

inline void add_edge(int x, int y, int z) { e[en] = edge(y, z, st[x]), st[x] = en++; }

int n, m;

int S, T;

bool bad[maxn + 5];

int buffer[maxn + 5];

int dfn_cnt = 0;
int dfn[maxn + 5], low[maxn + 5];

bool has_T[maxn + 5];

void get_bad_vertices(int x) {
    if (x == T)
        has_T[x] = 1;
    dfn[x] = low[x] = dfn_cnt++;
    int cnt = 0;
    for (int i = st[x]; ~i; i = e[i].nxt) {
        int y = e[i].id;
        if (!~dfn[y]) {
            get_bad_vertices(y);
            if (has_T[y])
                has_T[x] = 1;
            if (has_T[y] || low[y] < dfn[x]) {
                cnt += buffer[x];
                chkmin(low[x], low[y]);
            }
            buffer[x] = 0;
        } else if (dfn[y] < dfn[x]) {
            ++cnt;
            ++buffer[y];
            chkmin(low[x], dfn[y]);
        }
    }
    if (cnt <= 2 && x != S && x != T) {
        bad[x] = 1;
    }
}

LL dis[maxn + 5];

bool on_path[maxm + 5];

int pathn;
int path[maxn + 5];
int id[maxn + 5];

inline void get_path() {
    static bool vis[maxn + 5];
    static int pre[maxn + 5];
    priority_queue<pair<LL, int>, vector<pair<LL, int> >, greater<pair<LL, int> > > q;
    memset(dis, oo, sizeof(dis[0]) * n);
    memset(vis, 0, sizeof(vis[0]) * n);
    q.push(mp(0, S));
    dis[S] = 0;
    while (!q.empty()) {
        int x = q.top().y;
        q.pop();
        if (vis[x])
            continue;
        vis[x] = 1;
        for (int i = st[x]; ~i; i = e[i].nxt) {
            int y = e[i].id;
            if (!bad[y] && chkmin(dis[y], dis[x] + e[i].g)) {
                pre[y] = i ^ 1;
                q.push(mp(dis[y], y));
            }
        }
    }

    memset(on_path, 0, sizeof(on_path[0]) * m);
    pathn = 0;
    int cur = T;
    while (cur != S) {
        path[pathn++] = cur;
        on_path[pre[cur] >> 1] = 1;
        cur = e[pre[cur]].id;
    }
    path[pathn++] = cur;
    reverse(path, path + pathn);

    memset(id, -1, sizeof(id[0]) * n);
    REP(i, 0, pathn) id[path[i]] = i;
}

int fa[maxn + 5];

int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }

int lim[maxn + 5];

inline bool get_lim() {
    static bool leftmost[maxn + 5], rightmost[maxn + 5];
    static bool vis[maxn + 5];
    memset(leftmost, 0, sizeof(leftmost[0]) * pathn);
    memset(rightmost, 0, sizeof(rightmost[0]) * pathn);
    memset(vis, 0, sizeof(vis[0]) * n);
    REP(i, 0, pathn) {
        if (!vis[get(path[i])]) {
            leftmost[i] = 1;
            vis[get(path[i])] = 1;
        }
    }
    memset(vis, 0, sizeof(vis[0]) * n);
    for (int i = pathn - 1; i >= 0; --i) {
        if (!vis[get(path[i])]) {
            rightmost[i] = 1;
            vis[get(path[i])] = 1;
        }
    }
    if (leftmost[pathn - 1] || rightmost[0])
        return 0;
    REP(i, 0, pathn + 1) lim[i] = pathn;
    int lst = -1;
    REP(i, 0, pathn) {
        if (leftmost[i])
            lst = i;
        if (rightmost[i]) {
            if (lst > 0 && i + 1 < pathn) {
                lim[lst - 1] = i + 1;
            }
        }
    }
    for (int i = pathn - 1; i >= 0; --i) {
        chkmin(lim[i], lim[i + 1]);
    }
    return 1;
}

inline LL work() {
    static int vis[maxn + 5];
    static int info[maxn + 5];
    priority_queue<pair<LL, pair<int, int> >, vector<pair<LL, pair<int, int> > >,
                   greater<pair<LL, pair<int, int> > > >
        q;
    memset(vis, 0, sizeof(vis[0]) * n);
    q.push(mp(0, mp(S, 0)));
    while (!q.empty()) {
        LL d = q.top().x;
        int x = q.top().y.x, lst = q.top().y.y;
        q.pop();
        if (x == T) {
            return d;
        }
        if (!vis[x]) {
            info[x] = lst;
        } else if (vis[x] == 1) {
            if (~id[x]) {
                if (lim[lst] <= lim[info[x]])
                    continue;
            } else {
                if (lst >= info[x])
                    continue;
            }
        } else if (vis[x] > 1)
            continue;
        ++vis[x];
        for (int i = st[x]; ~i; i = e[i].nxt) {
            int y = e[i].id;
            if (bad[y])
                continue;
            if (~id[y]) {
                if (id[y] == lst)
                    continue;
                if (!on_path[i >> 1]) {
                    q.push(mp(d + e[i].g, mp(y, id[y])));
                } else if (id[y] < lim[lst]) {
                    q.push(mp(d + e[i].g, mp(y, lst)));
                }
            } else {
                if (~id[x])
                    q.push(mp(d + e[i].g, mp(y, id[x])));
                else
                    q.push(mp(d + e[i].g, mp(y, lst)));
            }
        }
    }
    return -1;
}

int main() { 
    Read(n), Read(m);
    memset(st, -1, sizeof(st[0]) * n), en = 0;
    REP(i, 0, m) {
        int x, y, z;
        Read(x), Read(y), Read(z), --x, --y;
        add_edge(x, y, z);
        add_edge(y, x, z);
    }
    Read(S), Read(T), --S, --T;

    memset(buffer, 0, sizeof(buffer[0]) * n);
    memset(dfn, -1, sizeof(dfn[0]) * n);
    memset(bad, 0, sizeof(bad[0]) * n);
    memset(has_T, 0, sizeof(has_T[0]) * n);
    dfn_cnt = 0;
    get_bad_vertices(S);

    get_path();

    REP(i, 0, n) fa[i] = i;
    REP(i, 0, n)
    for (int j = st[i]; ~j; j = e[j].nxt)
        if (!on_path[j >> 1])
            fa[get(i)] = get(e[j].id);

    bool not_connected = 0;
    REP(i, 0, n) if (get(i) != get(0)) {
        not_connected = 1;
        break;
    }

    if (not_connected) {
        if (!get_lim())
            printf("-1\n");
        else
            printf("%lld\n", work());
    } else
        printf("%lld\n", dis[T]);
    return 0;
}

詳細信息

Test #1:

score: 5
Accepted
time: 2ms
memory: 28132kb

input:

2000 3845
382 752 1
1749 768 362685093
192 155 200269188
1412 176 1
1236 1618 1
261 284 189019852
890 1388 1120946
233 324 3022034
1917 1771 130025452
1809 1500 1947
185 232 746253829
1137 1922 2
1191 255 11199276
1421 1442 1
1623 816 1
1280 1582 453986608
1333 1855 24686226
1619 791 524136834
1782 267 446907559
137 49 68397575
833 766 428943465
1743 1567 1681856
870 158 127
1013 714 5300
195 1266 84035377
1341 321 5
1754 511 223905375
529 1703 1
394 1497 196887099
1404 1447 7318150
558 1850 717...

output:

134121129003

result:

ok answer is '134121129003'

Test #2:

score: 5
Accepted
time: 3ms
memory: 32296kb

input:

2000 3813
1269 1307 277362319
16 1800 261162
619 1121 4911855
845 1628 182659018
1342 1671 523343891
43 1559 424017
1646 598 1
1235 1452 5087666
958 1430 131571568
1345 1977 10947710
885 302 666376352
1196 56 352475081
1611 677 176592008
1002 475 1
1236 1654 242172993
241 989 865
103 1180 1
215 1951 538638386
1910 554 88310479
1239 814 132771
1884 1076 1239
1972 1134 129306463
1693 1793 166110
577 250 1
745 1292 1
829 217 4577
1548 995 1
1465 121 159714226
437 406 1
1699 1704 1
790 244 262197
59...

output:

138140854400

result:

ok answer is '138140854400'

Test #3:

score: 5
Accepted
time: 2ms
memory: 32344kb

input:

2000 3823
1270 460 221408836
1032 1152 4
333 106 441182807
1087 565 132001044
1238 467 248623737
1111 1599 192202789
1161 1704 5370107
1893 1898 761361694
228 1973 1
353 571 6
1600 1780 257614452
218 750 954445005
1942 949 272727
460 1671 315826797
279 604 1
1586 1549 182175601
1275 4 8731
1006 575 823994201
1058 274 269440220
1466 884 994676808
1641 1978 828769977
1323 553 40560273
158 1596 424727435
945 1106 1
1636 1815 191093494
365 1032 6
1050 1549 571457295
334 224 106399304
396 1171 170609...

output:

134118694716

result:

ok answer is '134118694716'

Test #4:

score: 5
Accepted
time: 0ms
memory: 30224kb

input:

2000 3825
1068 852 270912535
242 1849 181978822
153 1952 10004
1847 1247 353762544
439 1953 175213656
1516 31 768392654
1701 1725 18697901
1241 310 188712734
1707 377 113778691
1630 459 2
488 361 242496365
1558 1667 11545
841 174 17711495
1240 1978 227924190
620 166 4
1963 1845 107272881
621 99 24
1940 1575 149946189
1015 401 861610
1662 1902 31323618
898 9 832
1733 779 1
993 633 404656839
748 1985 32133
352 312 386735
212 1004 186634990
785 232 12939
1826 1077 133049804
1584 1810 857387600
1166...

output:

135181884998

result:

ok answer is '135181884998'

Test #5:

score: 5
Accepted
time: 8ms
memory: 28300kb

input:

2000 3828
1933 718 739678288
1147 497 195051551
228 1307 1
293 237 2
752 1106 976566039
1135 544 395818768
67 43 1
461 1993 38863780
1942 25 643685291
1542 630 150325791
247 167 38129947
684 1874 1
239 212 4
430 372 6319
1786 1951 73672621
221 1474 58774856
834 261 2730
144 1413 1
996 595 958104736
990 371 615243992
625 893 191559365
239 494 1
415 1895 772307916
1103 1866 5
753 62 1
974 603 5
966 1040 707769111
1399 1219 1
690 192 32753676
1038 1115 474608305
1460 1119 9199888
616 803 442678709
...

output:

133250447316

result:

ok answer is '133250447316'

Test #6:

score: 5
Accepted
time: 3ms
memory: 32344kb

input:

2000 3831
745 1025 72660523
17 1923 78369483
1739 210 615012778
148 1675 90932234
1676 1197 129874536
1500 1142 44945436
217 386 284154
1712 1268 240818882
533 1468 320
1530 753 492797879
765 1353 54317386
1520 1552 2109786
1846 1507 142535218
1835 1304 134772621
921 205 224272187
715 195 4504351
222 946 876080
879 1394 127411137
396 1558 44383123
642 733 1
71 149 4246
440 1420 104499043
1605 1499 735004681
909 895 1317787
1440 1248 9848
747 1633 169979945
173 494 167710396
989 1898 219647438
78...

output:

134653840656

result:

ok answer is '134653840656'

Test #7:

score: 5
Accepted
time: 306ms
memory: 65084kb

input:

500000 889698
412220 407980 499428456
158832 239591 499428456
182707 117557 499428456
214936 123420 499428456
365872 181112 499428456
30686 198797 499428456
312244 385027 499428456
284487 46167 499428456
217227 443514 499428456
347857 111938 499428456
108078 492305 499428456
162160 148152 499428456
266404 273748 499428456
263521 244308 499428456
196644 379683 499428456
182486 266960 499428456
460357 330357 499428456
143968 431827 499428456
494032 91893 499428456
412506 195143 499428456
209738 29...

output:

-1

result:

ok answer is '-1'

Test #8:

score: 5
Accepted
time: 281ms
memory: 65444kb

input:

500000 889666
88463 342604 382544584
50508 395252 382544584
128944 42145 382544584
142177 278396 382544584
403758 1554 382544584
362313 245693 382544584
337218 91514 382544584
2820 149982 382544584
475533 212931 382544584
277184 397094 382544584
484709 87632 382544584
101778 452812 382544584
31619 478716 382544584
385155 10224 382544584
75094 290938 382544584
275406 177796 382544584
318987 265728 382544584
373872 334893 382544584
372292 210909 382544584
261000 1513 382544584
482724 458222 382544...

output:

53147301599704

result:

ok answer is '53147301599704'

Test #9:

score: 5
Accepted
time: 318ms
memory: 64824kb

input:

500000 889500
17217 253061 855595304
304789 334990 855595304
330105 53808 855595304
24564 218408 855595304
121664 490479 855595304
250105 234943 855595304
99628 83788 855595304
36542 384892 855595304
290146 56887 855595304
415420 88878 855595304
250575 370062 855595304
245581 15004 855595304
133273 112463 855595304
145946 104158 855595304
96719 77145 855595304
467548 129229 855595304
336934 179493 855595304
425877 127602 855595304
249339 206989 855595304
53141 57505 855595304
466509 442715 85559...

output:

118695880928616

result:

ok answer is '118695880928616'

Test #10:

score: 5
Accepted
time: 319ms
memory: 65140kb

input:

500000 889582
454310 375253 738711432
124500 417665 738711432
303268 163821 738711432
375036 459659 738711432
301583 368655 738711432
438271 391586 738711432
82908 376663 738711432
123789 325867 738711432
197623 238951 738711432
253554 409791 738711432
488826 81355 738711432
429142 157251 738711432
274797 317564 738711432
240172 466676 738711432
438400 165310 738711432
79513 427266 738711432
350746 172914 738711432
18197 104892 738711432
343673 490072 738711432
45172 234258 738711432
40466 39854...

output:

102610711461960

result:

ok answer is '102610711461960'

Test #11:

score: 5
Accepted
time: 1017ms
memory: 120152kb

input:

500000 970996
159170 122348 1
280902 145846 1
306211 24651 1
88424 333838 1
322035 119018 1
1610 393965 1
204248 294711 358492490
288255 203073 1
71527 147298 123453960
492803 5795 223
229170 146455 1
463197 345469 1
163991 428079 1
359263 51301 1
421528 227565 957
46453 250036 1
487873 113177 1
486874 86175 1
323053 124350 1
34548 479908 430352777
381869 325061 1
23493 428013 1
495443 329287 1
456792 109800 734958124
21546 57726 49379068
254194 243445 1
362081 347613 1
403284 496554 1
257433 15...

output:

5860994850909

result:

ok answer is '5860994850909'

Test #12:

score: 5
Accepted
time: 1034ms
memory: 99524kb

input:

500000 971032
115783 252285 1
465444 224227 6
217615 122881 1
212250 340359 1
381692 26753 27904
429751 27996 538682339
229314 469878 1
7985 40852 1
277746 199722 1
279251 57570 1
274638 187821 1
9222 335353 1
84220 185452 1
82420 97974 114118
337199 445497 14820
442671 73197 7995
328727 40707 826406631
372790 99877 811600135
324046 363977 1
148505 468564 1
351147 33262 685086422
50301 244999 1
128848 132819 1
350268 456785 1
410952 396619 1
444361 407560 1
173787 254606 151135
202498 183007 1
2...

output:

5979754647875

result:

ok answer is '5979754647875'

Test #13:

score: 5
Accepted
time: 1247ms
memory: 111628kb

input:

500000 970905
95485 91092 1
372030 437825 1
311904 227741 487554784
420925 492435 1
171872 335733 1
413562 275040 1
48577 117737 1
313317 386004 360250873
251801 56752 850411041
176471 193862 773
377095 429850 1
231630 107647 283939492
52019 395817 1
355988 421294 869718039
64841 118126 979910530
447555 240749 1
163258 69636 1
249352 266290 1
427220 77737 397492939
312522 381190 357735775
481316 461496 1
111568 95549 927
37642 309117 996668953
60898 45432 1
496393 270576 111551018
449105 235221 ...

output:

5913452707639

result:

ok answer is '5913452707639'

Test #14:

score: 5
Accepted
time: 1248ms
memory: 111680kb

input:

500000 970992
94750 47862 641272697
173456 220989 1
34685 113862 4
116058 99494 1
102996 191245 186350796
143392 175541 9
166102 68291 911510828
175503 199733 909168328
354558 469817 288579804
354527 110456 1
263220 253494 326018395
140412 448661 1
301473 343155 706602971
149351 457770 1
337053 292331 1
25959 435912 1
382918 36121 451231620
73706 50670 1
214278 250879 1
194516 327007 485131555
413076 408574 164429153
28187 157924 1
402087 349180 6
228915 458434 208332218
170728 119539 285019885
...

output:

5898999991008

result:

ok answer is '5898999991008'

Test #15:

score: 5
Accepted
time: 1317ms
memory: 111724kb

input:

500000 970852
137047 205324 1
312328 35828 1
242221 383587 427593843
142623 184077 309394906
208198 90167 922782015
156619 477588 14944666
151572 446806 1
326606 134904 1
465271 37039 1
266406 125472 1
122953 464090 379
41712 362091 1
424327 302149 1
424465 107198 206627661
103964 67048 163
101342 417451 1
240512 71163 731222789
336116 127354 1
136618 226477 1
81101 368396 980994772
315456 378943 535298065
394676 239879 1
470091 161980 899751248
318075 328405 297328051
490537 355771 871574555
32...

output:

6018366516551

result:

ok answer is '6018366516551'

Test #16:

score: 5
Accepted
time: 701ms
memory: 69572kb

input:

500000 959403
6507 367806 850341350
36869 59690 67
20756 268956 425995925
384478 127906 2
364049 100536 1
369975 153712 19400997
117523 265664 1
457574 23 805942267
482455 389707 160636821
298125 142295 206942709
289174 51593 56763439
432878 313640 249061366
400416 390030 4
487587 35240 972671972
76342 47609 104
364806 351697 88597498
477568 427992 1
196718 459235 27747533
200938 458944 5
50828 306493 1
295997 487299 234925184
482878 65517 1
366284 264184 17
174063 42144 193350403
357859 49481 2...

output:

33280951789769

result:

ok answer is '33280951789769'

Test #17:

score: 5
Accepted
time: 667ms
memory: 69528kb

input:

500000 959429
65141 71416 110109451
92695 83799 6
266981 421290 449719409
4938 23167 1
335697 357809 247869044
43040 223964 2
388744 26145 1
440267 74224 28482479
253309 298986 67897188
481999 181973 1
366665 153587 752798493
340278 421171 89443207
193320 53820 1
11495 67348 54041386
343103 273144 127503730
498798 365775 1
311569 74065 1
486243 163789 62517990
164198 178333 995944345
203878 437082 132851825
452803 383045 550663043
65831 85170 285567011
351056 82386 128792740
166140 160820 1
4882...

output:

33280871544058

result:

ok answer is '33280871544058'

Test #18:

score: 5
Accepted
time: 624ms
memory: 69560kb

input:

500000 959224
311045 74231 654
2450 321772 52033
490860 219449 1
182619 154257 35502539
339285 169887 123648903
443080 351262 134321942
56933 44672 332954176
14064 332642 1
443040 165457 24594891
62438 234761 129992922
494078 89935 1
142211 432787 106388739
10039 4791 138147678
225152 116732 13107091
364366 231493 3
50523 307054 222382380
133828 171224 268302923
437882 103916 1
45696 153527 219181606
86840 91811 253980913
148107 97687 233266945
310037 257563 237493078
339685 422146 72861585
2086...

output:

33339043614821

result:

ok answer is '33339043614821'

Test #19:

score: 5
Accepted
time: 706ms
memory: 69584kb

input:

500000 959433
99510 267488 112661190
355351 358725 65050330
399590 317864 13
198999 367969 475485612
159372 167207 65208983
430286 328666 476390860
4325 349167 2
20161 223579 346649178
480983 317598 272193902
29296 251581 105470410
493625 346440 45
393054 281607 215047915
225199 188080 199076839
72704 127902 1976046
372871 277245 1
325780 452700 604300535
293430 110553 1
256183 122974 1
56885 218384 109800091
341562 65667 1
380692 185637 1
306659 45996 56105743
8372 45063 7
250015 7193 989792105...

output:

33255817749539

result:

ok answer is '33255817749539'

Test #20:

score: 5
Accepted
time: 573ms
memory: 69532kb

input:

500000 959659
217672 276229 6
329164 411970 135170666
342422 146298 391789563
474689 165546 217874734
402611 225401 6825828
186743 227563 12
67384 114722 4
448351 237508 1
174763 496208 1
206819 211755 3
91807 19129 114701350
446246 324623 192
100550 16128 327840431
492398 362096 436
126305 122021 12
448015 178830 7
96823 422757 3725
440677 49959 1
63616 484940 191
275497 40671 3
476003 53922 71789918
422086 158863 838
12359 347362 2
236957 55613 1
425307 368353 220977542
364618 243661 638009180...

output:

33290733109033

result:

ok answer is '33290733109033'

Extra Test:

score: 0
Extra Test Passed