QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132394#853. Flat Organization_LAP_AC ✓1063ms35072kbC++143.0kb2023-07-29 19:48:072023-07-29 19:48:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-29 19:48:11]
  • 评测
  • 测评结果:AC
  • 用时:1063ms
  • 内存:35072kb
  • [2023-07-29 19:48:07]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 2005; const long long INF = 0x3f3f3f3f3f3f3f3fLL;
int n, d[N][N];

int pre[N], low[N], sccno[N], dfs_clock;
stack<int> stk; int scc_c;
inline void Tarjan(int u) {
    pre[u] = low[u] = ++dfs_clock; stk.push(u);
    for(int v = 1; v <= n; v ++) if(d[u][v]) {
        if(!pre[v]) {
            Tarjan(v); low[u] = min(low[u], low[v]);
        } else if(!sccno[v]) low[u] = min(low[u], pre[v]);
    }
    if(low[u] >= pre[u]) {
        int y; scc_c ++;
        do {
            y = stk.top(); stk.pop(); sccno[y] = scc_c;
        } while(y != u);
    }
}

struct edge {
    int from, to, dist;
    edge(int u = 0, int v = 0, int d = 0) : from(u), to(v), dist(d) {}
};
int G[N][N];
int ind[N], outd[N];
inline void Build() {
    for(int i = 0; i <= n + 1; i ++)
        for(int j = 0; j <= n + 1; j ++)
            G[i][j] = -1;
    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= n; j ++) if(d[i][j] > 0) {
        G[i][j] = 0;
        if(sccno[i] != sccno[j]) G[j][i] = d[i][j], ind[sccno[j]] ++, outd[sccno[i]] ++;
    }
    pair<int, int> ans = {-1, -1};
    for(int i = 1; i <= scc_c; i ++) {
        if(!ind[i]) ans.first = i;
        if(!outd[i]) ans.second = i;
    }
    for(int i = 1; i <= n; i ++) if(sccno[i] == ans.second) G[0][i] = 0;
    for(int i = 1; i <= n; i ++) if(sccno[i] == ans.first) G[i][n + 1] = 0;
}

int prv[N]; long long dist[N]; bool vis[N];
inline void Dijkstra(int S) {
    dist[S] = 0;
    for(int i = 0; i < n + 2; i ++) {
        int u = -1;
        for(int j = 0; j <= n + 1; j ++) if(!vis[j] && (u == -1 || dist[j] < dist[u])) u = j;
        vis[u] = true;
        for(int j = 0; j <= n + 1; j ++) 
            if(G[u][j] >= 0 && dist[j] > dist[u] + G[u][j]) {
                dist[j] = dist[u] + G[u][j];
                prv[j] = u;
            }
    }
}

inline void Init() {
    memset(pre + 1, 0, sizeof(int) * n);
    memset(low + 1, 0, sizeof(int) * n);
    memset(sccno + 1, 0, sizeof(int) * n);
    memset(ind + 1, 0, sizeof(int) * n), memset(outd + 1, 0, sizeof(int) * n);
    memset(dist, 0x3f, sizeof(long long) * (n + 2));
    memset(vis, false, sizeof(bool) * (n + 2));
    dfs_clock = scc_c = 0;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);

    int T; cin >> T;
    while(T --) {
        cin >> n; Init();
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                cin >> d[i][j];
        if(n == 2) {cout << "NO\n"; continue; }
        cout << "YES\n";
        for(int i = 1; i <= n; i ++) 
            if(!pre[i]) Tarjan(i);
        if(scc_c == 1) {cout << "0 0\n"; continue; }
        Build(); Dijkstra(0);

        vector<pair<int, int>> ans;
        int u = n + 1;
        while(u) {
            if(G[prv[u]][u] > 0) ans.push_back({u, prv[u]});
            u = prv[u];
        }
        cout << ans.size() << ' ' << dist[n + 1] << '\n';
        for(auto &pr : ans) cout << pr.first << ' ' << pr.second << '\n';
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0

output:

YES
2 10
1 4
4 5

result:

ok OK!

Test #2:

score: 0
Accepted
time: 11ms
memory: 5820kb

input:

100
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0
1
0
2
0 0
7 0
2
0 1000000000
0 0
3
0 0 5
6 0 0
0 7 0
3
0 4 6
0 0 0
0 1 0
3
0 4 0
0 0 0
6 3 0
3
0 0 0
10 0 6
3 0 0
3
0 1999999998 1999999999
0 0 1999999998
0 0 0
50
0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...

output:

YES
2 10
1 4
4 5
YES
0 0
NO
NO
YES
0 0
YES
1 4
1 2
YES
1 3
3 2
YES
2 9
2 3
3 1
YES
1 1999999999
1 3
YES
3 602
4 33
11 47
34 39
YES
3 649
27 12
32 45
17 29
YES
5 1209
1 18
14 35
35 4
4 25
25 12
YES
3 844
3 8
1 41
14 21
YES
3 866
17 35
35 44
46 26
YES
4 1066
10 28
36 24
24 2
37 8
YES
3 1122
4 17
43 19...

result:

ok OK!

Test #3:

score: 0
Accepted
time: 113ms
memory: 8440kb

input:

50
200
0 40000001 47000001 35500000 37500501 0 0 0 26000000 29500501 0 22000501 6000000 33000000 0 73500000 68500501 50500500 32000000 12000001 0 0 49000000 0 67000500 70000000 5500500 60500001 0 28000001 35000500 31000001 0 36500001 69500000 57000001 65500000 63500000 0 64000500 51000001 56500000 6...

output:

YES
24 48002011
125 39
78 174
174 93
22 21
195 1
183 71
60 170
124 63
132 110
173 97
97 120
72 142
142 10
10 52
32 54
95 51
2 164
23 123
36 135
73 57
150 37
25 138
26 47
112 85
YES
30 42342994
114 109
146 162
79 29
72 103
166 121
32 155
143 77
50 173
173 26
28 73
61 96
163 142
34 36
14 189
70 6
115 ...

result:

ok OK!

Test #4:

score: 0
Accepted
time: 1030ms
memory: 35072kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

YES
2 1023134
1609 106
1628 1
YES
2 1792364
1445 1040
594 1232
YES
325 416316941
532 682
1692 1737
1737 414
1682 715
538 254
1850 1994
1994 73
914 688
1947 1566
1566 1177
519 10
203 1577
1330 721
1287 786
786 1686
1291 1918
1773 155
1936 1911
1911 1522
1119 584
584 656
1851 1250
1020 417
1830 189
18...

result:

ok OK!

Test #5:

score: 0
Accepted
time: 1036ms
memory: 35016kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

YES
5 996500000
1609 600
1661 963
1559 1040
1910 179
179 1
YES
13 994553657
324 19
19 1668
1825 1216
1216 912
912 925
925 536
1342 1391
1391 400
1763 810
65 1760
1760 132
132 1172
1660 813
YES
330 434538620
264 1327
251 832
947 5
1967 1023
1023 1016
113 641
951 1138
1818 392
903 1622
1622 1934
508 1...

result:

ok OK!

Test #6:

score: 0
Accepted
time: 1063ms
memory: 35012kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2289820 14688249 0 0 0 0 9856840 4321803 39208 583215 0 0 0 0 0 0 0 0 0 0 0 10857808 0 0 0 0 0 0 0 11712810 0 0 0 0 0 5848230 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11045028 0 0 5712236 72208 9245030 0 0 0 0 0 0 0 10580003 18361832 0 0 20995247 0 24081817 0 0 0 27...

output:

YES
1999 449308
161 1784
1784 659
659 1794
1794 1490
1490 707
707 384
384 432
432 1643
1643 1850
1850 510
510 992
992 721
721 241
241 166
166 1518
1518 230
230 1254
1254 607
607 736
736 126
126 1262
1262 1929
1929 1499
1499 182
182 1803
1803 787
787 410
410 626
626 1856
1856 1406
1406 484
484 844
84...

result:

ok OK!

Test #7:

score: 0
Accepted
time: 1054ms
memory: 35024kb

input:

5
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 306240 4975575 0 0 0 0 2735274 794096 689 39371 0 0 0 0 0 0 0 0 0 0 0 3162279 0 0 0 0 0 0 0 3543126 0 0 0 0 0 1250019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3244419 0 0 1206671 1713 2484549 0 0 0 0 0 0 0 3041751 6954466 0 0 8503070 0 10445400 0 0 0 12655 512079 0 ...

output:

YES
970 6111
161 659
659 1490
1490 432
384 1643
1643 1850
1850 721
721 1518
1518 607
1254 126
126 1499
1499 182
182 410
410 1856
1856 1406
1406 203
203 1457
1457 1764
1764 1105
1105 947
947 705
705 1195
1195 1146
1146 1384
1384 944
944 485
485 36
36 1537
1401 118
118 1942
1942 290
290 1935
1935 1361...

result:

ok OK!

Test #8:

score: 0
Accepted
time: 772ms
memory: 34996kb

input:

45
2000
0 0 0 7 0 7 0 0 0 0 0 0 0 0 0 7 0 0 0 7 7 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 7 0 0 0 7 7 0 0 0 7 7 7 7 0 7 0 0 7 0 0 0 0 0 7 7 0 0 0 7 7 7 7 0 7 0 0 0 7 0 7 0 0 7 0 0 0 0 0 0 0 7 7 7 0 7 7 0 7 7 0 0 0 0 7 0 0 0 0 0 7 7 7 0 0 7 0 0 0 7 0 7 0 0 0 7 0 0 0 0 7 7 0 0 7 0 0 0 0 0 7 0 7 0 0 0 7 ...

output:

YES
2 21
1851 1
1 946
YES
2 21
1184 1
1 1401
YES
2 21
87 1
1 65
YES
2 21
10 1
1 2
YES
2 21
2 1
1 5
YES
2 21
13 1
1 2
YES
2 21
1 4
4 13
YES
2 21
3 1
1 15
YES
2 21
2 1
1 11
YES
2 21
5 3
3 7
YES
2 21
9 3
3 1
YES
2 21
9 2
2 11
YES
2 21
2 3
3 5
YES
2 21
5 1
1 11
YES
2 21
1 8
8 3
YES
2 21
6 1
1 16
YES
2 2...

result:

ok OK!