QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132386#853. Flat Organization_LAP_WA 12ms3988kbC++143.1kb2023-07-29 19:09:252023-07-29 19:09:29

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:09:29]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:3988kb
  • [2023-07-29 19:09:25]
  • 提交

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) {}
};
vector<edge> G[N];
int ind[N], outd[N];
inline pair<int, int> Build() {
    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= n; j ++) 
        if(d[i][j] && sccno[i] != sccno[j]) {
            G[sccno[i]].emplace_back(edge(i, j, 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;
    }
    if(ans.first == -1 || ans.second == -1) assert(false);
    return ans;
}

typedef pair<long long, int> pli;
pair<int, int> prv[N]; long long dist[N]; bool vis[N];
inline void Dijkstra(int S) {
    priority_queue<pli, vector<pli>, greater<pli>> Q;
    dist[S] = 0; Q.push({dist[S], S});
    while(!Q.empty()) {
        int u = Q.top().second; Q.pop(); if(vis[u]) continue;
        vis[u] = true;
        for(auto &e : G[u]) { int v = sccno[e.to];
            if(dist[v] > dist[u] + e.dist) {
                dist[v] = dist[u] + e.dist; prv[v] = {e.from, e.to};
                Q.push({dist[v], v});
            }
        }
    }
}

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 + 1, 0x3f, sizeof(long long) * n);
    memset(vis + 1, 0, sizeof(bool) * n);
    for(int i = 1; i <= n; i ++) G[i].clear();
    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; }
        pair<int, int> P = Build(); Dijkstra(P.first);
        
        vector<pair<int, int>> ans;
        int u = P.second;
        while(u != P.first) {
            ans.emplace_back(prv[u]);
            u = sccno[prv[u].first];
        }
        cout << ans.size() << ' ' << dist[P.second] << '\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: 3596kb

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
4 5
1 4

result:

ok OK!

Test #2:

score: -100
Wrong Answer
time: 12ms
memory: 3988kb

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

result:

wrong answer Test 48: The solution is not optimal